Can you just update a partial view instead of full page post?

后端 未结 5 1658
清酒与你
清酒与你 2020-12-23 17:04

Is there a way to submit a partial view form in asp.net mvc without reloading the parent page, but reloading the partial view only to its new state? Similar to how knockout.

5条回答
  •  误落风尘
    2020-12-23 17:07

    Not without jQuery.

    What you would have to do is put your Partial in a div, something like:

    @Html.Partial("YourPartial")

    Then, to update (for example clicking a button with the id button), you could do:

    $("#button").click(function () {
       $.ajax({
           url: "YourController/GetData",
           type: "get",
           data: $("form").serialize(), //if you need to post Model data, use this
           success: function (result) {
               $("#partial").html(result);
           }
       });
    })
    

    Then your action would look something like:

    public ActionResult GetData(YourModel model) //that's if you need the model
    {
        //do whatever
    
        return View(model);
    }
    

提交回复
热议问题