mvc 部分视图 ajax 更新将部分视图作为页面返回

发布于 2025-01-06 15:19:07 字数 840 浏览 1 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往事风中埋 2025-01-13 15:19:07

由于 AJAX 表单中的 UpdateTargetId = "wingrid" 选项,您似乎已经在这样做了。只需确保清除在模型状态的 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);
}

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

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

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:

[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);
}

Also don't forget to include the jquery.unobtrusive-ajax.js script to your page if you want your Ajax.* helpers to work:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文