页面上有多个Ajax.BeginForm

时间:2013-04-22 16:53:02

标签: c# ajax asp.net-mvc-4

我需要实现多个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 OnSuccessOnSuccess2

有什么猜测可以做到吗?

1 个答案:

答案 0 :(得分:2)

使用View()PartialView(),MVC会尝试查找名称等于操作名称的视图。

所以,只是不要返回View(model) 返回EmptyResult()或JSON对象。