我終於設法使用Ajax來更新我的PartialView。部分視圖的目的是一個側欄小部件,用於顯示註冊內容並允許從註冊中刪除項目。如何優雅地處理禁用JavaScript時的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的網址,而不會顯示其他內容。我想要發生的是,對於JavaScript禁用的用戶,他們會被重定向回發佈表單的原始頁面。
這是可以實現的嗎?
'Request.IsAjaxRequest'可以幫助您決定要渲染哪個視圖。 –