Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

asp.net core - Update the data in View with AJAX not working

I have diffrent <a> tags that normally pass a parameter to an action using asp-route="". Now i want to use AJAX t do this. each tag is supposed to pass an ID that comes from the model chats when the page loads up. (asp-route-ChatID="@Model.ID"). How can I achieve this in AJAX and pass that parameter? I tried using Data-ajax-update and Data-ajax-url but these option arent even available in Intellisense. Here is the method that i want to call

  public IActionResult Chat(string ChatID)
    {
        Chats userchats=new Chats(); // a class that holds chats
         //selecting data from the database......
        
        return View(userchats);//returns view with the model
    }

here is the <a> tag that gets clicked and it needs to pass a specific ID that comes from the model

<a > View Chat </a>

here is the <div> that needs to be updated depending on what <a> is clicked

    <div id="UpdateThis">  <!--show data that comes from the method ajax calls-->   </div>

How can I implement ajax here with tag helpers or any other way?

question from:https://stackoverflow.com/questions/65868561/update-the-data-in-view-with-ajax-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use PartialView instead of view if you want to update div.

public IActionResult Chat(string ChatID)
{
    Chats userchats=new Chats(); // a class that holds chats
    //selecting data from the database......
        
    return PartialView(userchats);//returns view with the model
}

View

<a id="btnView" data-id="1">View Chat</a>
<div id="UpdateThis"></div>

<script>
   $("#btnView").click(function () {
      $.post('@Url.Action("Chat")', { ChatID: $(this).data("id") }).done(function (e) {
          $("#UpdateThis").html(e);
      })
   });
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...