我需要实现多个 Ajax 表单。
html
@using (Ajax.BeginForm("PerformAction", "Home", new AjaxOptions { OnSuccess = "OnSuccess", OnFailure = "OnFailure" }))
{
<h3>This is a demo form #1.</h3>
@Html.LabelFor(model => model.FirstName)
@Html.TextBoxFor(model => model.FirstName, null, new { @class = "labelItem" })
<input type="submit" value="Submit" />
}
<br />
<br />
@using (Ajax.BeginForm("PerformAction2", "Home", new AjaxOptions { OnSuccess = "OnSuccess2", OnFailure = "OnFailure2" }))
{
<h3>This is a demo form #2. </h3>
@Html.LabelFor(model => model.FirstName)
@Html.TextBoxFor(model => model.FirstName, null, new { @class = "labelItem" })
<input type="submit" value="Submit" />
}
<br />
<br />
<script type="text/javascript">
function OnSuccess(response) {
alert(response);
}
function OnFailure2(response) {
alert("222 Whoops! That didn't go so well did it?");
}
function OnSuccess2(response) {
alert(response);
}
function OnFailure(response) {
alert("Whoops! That didn't go so well did it?");
}
</script>
C#
[AcceptVerbs("POST")]
public ActionResult PerformAction(FormCollection formCollection, Person model)
{
model.FirstName += " Form 1";
return View(model);
}
[AcceptVerbs("POST")]
public ActionResult PerformAction2(FormCollection formCollection, Person model)
{
model.FirstName += " Form 2";
return View(model);
}
但我面临着 ASP .NET MVC 4 正在寻找某种部分形式的错误。
我不想使用任何部分形式,只需要使用JavaScript OnSuccess
和OnSuccess2
猜猜它是怎么做到的?