我對返回JSON的方法使用AJAX調用。我怎樣才能讀取返回的JSON而不被重定向到一個新的空白頁面?如何防止瀏覽器在提交表單時重定向到新頁面?
[HttpPost]
public JsonResult Test()
{
return Json("JSON return test", JsonRequestBehavior.AllowGet);
}
@model List<POC.Web.Intranet.Models.AttachmentPropertyViewModel>
<div>
@using (Ajax.BeginForm("Test", "DMS", new AjaxOptions { HttpMethod = "POST", OnSuccess = "endMethod()", OnBegin = "beginMethod()" }))
{
<h6>Properties</h6>
for (int i = 0; i < Model.Count; i++)
{
@Html.HiddenFor(item => item[i].AttachmentPropertyId);
@Html.HiddenFor(item => item[i].AttachmentMetaDataId);
<label>@Model[i].AttachmentPropertyName</label>
@Html.TextBoxFor(item => item[i].AttachmentPropertyValue, htmlAttributes: new { @class = "form-control property-value-txtbox" });
<br />
}
<input type="submit" id="btnSaveChanges" value="Save Chnages" />
}
<hr />
</div>
<script>
function beginMethod() {
alert("Start");
}
function endMethod() {
alert("Finish");
// also here i want to read the incoming json
}
</script>
它被觸發'beginMethod'? –
輸入'type =「submit」'將表單提交給控制器。所以把它改爲'button' – Qsprec
這必須是stackoverflow上最常問的問題。 – philantrovert