mvc 部分视图 ajax 更新将部分视图作为页面返回
我有一个部分观点,即在向数据库添加项目时需要更新。
Index.cshtml:
@using (Ajax.BeginForm("Index", "WinEntry", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "wingrid", InsertionMode = InsertionMode.Replace}))
{
@Html.Partial("_winVenue")
@Html.Partial("_singleWin")
}
<div id="wingrid">
@Html.Partial("_wingrid")
</div>
_singleWin 有提交按钮
控制器:
[HttpPost]
public ActionResult Index(Win win)
{
win.dealerId = "1234567890";
win.posterid = "chris";
win.posttime = DateTime.Now;
wem.addWin(win);
IEnumerable<Win> w = wem.getVenueWins(win.venue,win.windate);
return PartialView("_wingrid",w);
}
当控制器返回部分视图 _wingrid 时,它会将其作为新页面返回以及我正在寻找的行为就像 wingrid div 内的更新面板。
任何帮助将不胜感激。
I have a partial view that upon adding an item to the database needs to update.
The Index.cshtml:
@using (Ajax.BeginForm("Index", "WinEntry", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "wingrid", InsertionMode = InsertionMode.Replace}))
{
@Html.Partial("_winVenue")
@Html.Partial("_singleWin")
}
<div id="wingrid">
@Html.Partial("_wingrid")
</div>
The _singleWin has the submit button
The controller:
[HttpPost]
public ActionResult Index(Win win)
{
win.dealerId = "1234567890";
win.posterid = "chris";
win.posttime = DateTime.Now;
wem.addWin(win);
IEnumerable<Win> w = wem.getVenueWins(win.venue,win.windate);
return PartialView("_wingrid",w);
}
When the controller returns the partial view _wingrid it returns it as a new page and the behavior I am looking for is like an update panel inside the wingrid div.
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 AJAX 表单中的
UpdateTargetId = "wingrid"
选项,您似乎已经在这样做了。只需确保清除在模型状态的 POST 控制器操作中修改的值即可。否则 HTML 助手仍然可以使用旧值:如果您想要
Ajax.*
,也不要忘记将jquery.unobtrusive-ajax.js
脚本包含到您的页面中。工作帮手:You already seem to be doing this thanks to the
UpdateTargetId = "wingrid"
option in your AJAX form. Just make sure that you clear values that you modify in your POST controller action form the modelstate. Otherwise HTML helpers could still use the old values:Also don't forget to include the
jquery.unobtrusive-ajax.js
script to your page if you want yourAjax.*
helpers to work: