3

我有一个部分观点,即在将项目添加到数据库时需要更新。

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 内的更新面板。

任何帮助,将不胜感激。

4

1 回答 1

5

UpdateTargetId = "wingrid"由于AJAX 表单中的选项,您似乎已经这样做了。只需确保从模型状态中清除在 POST 控制器操作中修改的值。否则 HTML 助手仍然可以使用旧值:

[HttpPost]
public ActionResult Index(Win win)
{
    ModelState.Remove("dealerId");
    win.dealerId = "1234567890";
    ModelState.Remove("posterid");
    win.posterid = "chris";
    ModelState.Remove("posttime");
    win.posttime = DateTime.Now;
    wem.addWin(win);
    IEnumerable<Win> w = wem.getVenueWins(win.venue,win.windate);
    return PartialView("_wingrid",w);
}

jquery.unobtrusive-ajax.js如果您希望Ajax.*助手工作,也不要忘记将脚本包含到您的页面中:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
于 2012-02-19T13:00:43.617 回答