【发布时间】:2014-03-22 23:22:21
【问题描述】:
我终于设法使用 Ajax 更新我的 PartialView。部分视图的目的是显示注册内容并允许从注册中删除项目的侧边栏小部件。
PartialView 的缩写版本如下:
<table id="item-entries">
@foreach (var item in Model.Items)
{
<tr>
<td>@item.Name</td>
<td>@item.Price</td>
<td>
@using (Ajax.BeginForm("RemoveItemEntry", "Registration", new AjaxOptions { UpdateTargetId = "item-entries" }))
{
<button type="submit" name="ItemId" value="@item.ItemId">×</button>
}
</td>
</tr>
}
</table>
下面是该动作的一个缩略示例:
[HttpPost]
public ActionResult RemoveItemEntry(ItemViewModel data)
{
// Perform logic to remove the item from the registration
// then return the PartialView with updated model
return PartialView("~/Views/Partials/ItemEntries.cshtml", model);
}
}
现在这很好用,但是我不想为禁用 JavaScript 的用户提供糟糕的体验。如果您在禁用 JavaScript 的情况下发布表单,则该操作仍会正确执行,但您会被重定向到呈现 PartialView 的 url,仅此而已。我希望发生的是,对于禁用 JavaScript 的用户,他们将被重定向回发布表单的原始页面。
这可以实现吗?
【问题讨论】:
-
Request.IsAjaxRequest可以帮助您决定要渲染哪个视图。
标签: c# asp.net-mvc asp.net-mvc-4 asp.net-ajax