views:

1883

answers:

2

I have a page that contains multiple inputs - I'm using Ajax.BeginForm to build a form for each set of inputs.

<% using (Ajax.BeginForm(new AjaxOptions() { InsertionMode = InsertionMode.InsertAfter, HttpMethod = "POST" }))
   { %>
     <input class="smallInput" type="text" name="duration"/>
     <input type="submit" value="Add" />
<% } %>

The controller looks like this

   [AcceptVerbs("POST")]
   public ActionResult AddExercise(FormCollection form)
   {

       // some save logic...

       return Content(string.Empty);

   }

This works - the data is submitted to my controller and is saved.

However, every time this happens my page is replaced by a blank page. If I explicitly return a View from my Action, that view appears instead. But I want it to submit the form, leaving my existing page in place as-is.

+1  A: 

You need to specify the id of the DOM element that you want to be updated:

<% using (Ajax.BeginForm(new AjaxOptions() { 
    UpdateTargetId = "someID", 
    InsertionMode = InsertionMode.InsertAfter, 
    HttpMethod = "POST" }))
{ %>
    <input class="smallInput" type="text" name="duration"/>
    <input type="submit" value="Add" />
<% } %>
Darin Dimitrov
+2  A: 

I wasn't including the MVC Ajax javascript files in my page. Adding those includes fixed the problem.

Jason