你能更新局部视图而不是整页帖子吗?

Ray*_*awn 33 asp.net-mvc jquery partial-views knockout.js

有没有办法在asp.net mvc中提交部分视图表单而无需重新加载父页面,但是只将部分视图重新加载到新状态?与knockout.js如何使用data-bind更新类似.

我的数据表使用可变数量的列/名称进行渲染,所以我不认为knockout.js是这个的选项,所以我试图使用局部视图.

mat*_*mmo 49

不是没有jQuery.

你需要做的就是把你的Partial放在一个div中,比如:

<div id="partial">
    @Html.Partial("YourPartial")
</div>
Run Code Online (Sandbox Code Playgroud)

然后,要更新(例如,单击带有id的按钮button),您可以执行以下操作:

$("#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);
       }
   });
})
Run Code Online (Sandbox Code Playgroud)

然后你的行动看起来像:

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

    return View(model);
}
Run Code Online (Sandbox Code Playgroud)


Dav*_*ich 13

实际上,如果你的Partial有一个子动作方法,你可以直接发布(甚至使用锚链接)到子动作,并获得类似Ajax的效果.我们在几个视图中执行此操作.

语法是

@Html.Action("MyPartial")
Run Code Online (Sandbox Code Playgroud)

儿童行动是

public ActionResult MyPartial()
{
    return PartialView(Model);
}
Run Code Online (Sandbox Code Playgroud)

如果您的表单发布到子操作

@using (Html.BeginForm("MyPartial"))
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

将使用子操作返回的部分视图更新部分视图.

Jquery仍然是更新部分的合法方式.但从技术上讲,你问题的答案是肯定的.

  • 在MVC5中,`Html.BeginForm("string")`调用扩展重载`BeginForm(这个HtmlHelper htmlHelper,对象routeValues)`并导致[`POST /?Length=N`](https://stackoverflow.com/)问题/ 4357856 /剃刀ActionLink的-autogenerating长度7合URL).我觉得这个答案"有效"是无意识的行为. (2认同)