我正在尝试使用Ajax.BeginForm更新我的应用程序中的表单。 更新正在进行,但在控制器中执行该功能后,它会重定向到具有控制器功能名称的视图。我不希望它重定向到另一个视图,而是希望显示成功或失败等警告消息。
<div id="target" ondrop="drop(event)" ondragover="allowDrop(event)">
@using (Ajax.BeginForm("Insert", "User", new AjaxOptions{
HttpMethod = "POST",
UpdateTargetId = "target",
OnSuccess = "success"
}))
{
<ul>
@foreach (var item2 in Model.sample)
{
<li draggable="true" ondragstart="drag(event)" class="Contentpropclass" value="@item2.name">
<input type="hidden" id="ID" name="ID" value="@item2.id"/>
<input type="hidden" value="@item2.name" name="Name" id="Name"/>
@item2.name
<input type="number" name="age" value="@item2.age" placeholder="age" />
<input type="number" name="siblings" value="@item2.siblings" placeholder="" />
</li>
}
</ul>
<br /> <br /> <br />
<br /> <br /> <br />
<br /> <br /> <br />
<button type="submit" value="submit">Save</button>
}
</div>
答案 0 :(得分:0)
您需要从控制器返回JSON。
<强> c#中强>
public ActionResult Controller1(String postdata1, object postdata2)
{
Boolean result = GetResultMethot();
var jsonResult = Json(result, JsonRequestBehavior.AllowGet);
return jsonResult;
}
<强> CSHTML 强>
@using (Ajax.BeginForm("Insert", "User", new AjaxOptions{
HttpMethod = "POST",
UpdateTargetId = "target",
OnSuccess = "AjaxResponse(data)"
}))
<强>的JavaScript 强>
function AjaxResponse(json) {
if(json != null && json.length > 0 && json == "true")
{
alert("true");
}
else
{
alert("false");
}
}