How to refresh only part of the Index page in MVC 5?

前端 未结 3 1865
执念已碎
执念已碎 2020-12-16 17:18

The Index page contains two partial views. One is for the user to enter the search criteria, the other to show the results. How to update the data in the Results view only ?

3条回答
  •  心在旅途
    2020-12-16 17:52

    I would put a form in your search partial. Then have some action post that, via JQuery, to your GetResults Action, which should return:

    return PartialView("~/Views/Shared/Results.cshtml", results); 
    

    Then, in the success callback of your JQuery post, spit the returned results to your $("#div_Results") like so:

    $.ajax({
        url: '/Home/GetResults',
        type: "POST",
        dataType: "html",
        data: $("#FormId").serialize(),
        success: function (data) {
            //Fill div with results
            $("#div_Results").html(data);
        },
        error: function () { alert('error'); }
    });
    

    Unless I have a typo or something, that should work. You'll need to replace the form selector with the Id of your form tag.

提交回复
热议问题