NoWar
NoWar

Reputation: 37633

Multiple Ajax.BeginForm on page

I need to implement multiple Ajax forms.

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);
        }

But I am facing the error about that ASP .NET MVC 4 is looking for some kind of partial form.

I don't want to use any partial form and just I need to get working JavaScript OnSuccess and OnSuccess2

Any guess how it could be done?

Upvotes: 0

Views: 3247

Answers (1)

Romias
Romias

Reputation: 14133

Using View() or PartialView(), MVC it will try to find a view with a name equals to the action name.

So, just don't return a View(model) Return a EmptyResult() or a JSON object.

Upvotes: 2

Related Questions