我对返回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