我已經閱讀了很多網絡上的類似問題,但仍然無法找出問題所在。我的Ajax.BeginForm
通過Controller
中的PartialView
返回消息,但該字符串會替換整個頁面。Ajax.BeginForm替換整個頁面
View
:
@using (Ajax.BeginForm("NoMoreItem", "ProductListing",
new AjaxOptions
{
HttpMethod = "get",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = string.Concat("NoMoreItemSection1-", counter)
}, new { @id = string.Concat("NoMoreItemForm1-", counter) }))
{
<input type="hidden" value="@Model.Email" name="email" />
<input type="hidden" value="@i.code" name="code" />
<input type="submit" value="notify me" />
}
</div>
我的控制器:
public ActionResult NoMoreItem(string email, string productCode)
{
string message;
_waitinglist.Save(email, productCode);
message = "Item added to Waiting List";
return PartialView("ItemAdded", message);
}
ItemAdded
查看:
@model string
<div>
@Model
</div>
我引用jquery.unobtrusive-ajax.js
文件在_Layout.cshtml
所以它似乎不是原因。
有沒有人有想法爲什麼消息取代整個頁面?
發生這種情況時,大部分時間都是因爲腳本加載不正確。看到這裏http://stackoverflow.com/questions/23895918/mvc5-ajax-beginform-and-the-correct-scripts-load-order – Jasen
在點擊按鈕之前呈現的HTML看起來像什麼? – Jasen
@Jasen與項目名稱和按鈕的表。我在佈局中包含了js文件,但沒有在ItemAdded視圖中使用佈局。也許這可能是原因?如果是這樣,我應該在部分視圖還是父視圖中引用js文件? – jkl