tags:

views:

22

answers:

1

In my ASP.net MVC View I have a dropdown that I want to get details on selection and asynchronously update a div. My aspx is as follows:

    <% using (Html.BeginForm("Index", "Portal", FormMethod.Post, new { id = "TheForm" }))
   {%>

    <h2>Index</h2>

    <% using (Ajax.BeginForm("Details", new AjaxOptions { UpdateTargetId = "mpkResults" }))
       { %>

    <%=Html.DropDownList("Docs", (IEnumerable<SelectListItem>)ViewData["Docs"],
        new { onchange = "document.getElementById('TheForm').submit();" })%>

     <p><input type="submit" value="Details" /></p>

    <% } %>

    <div id="mpkResults" style="margin:10px 0px 0px 0px;"></div> ...

The onchange event fires correctly on selection of the dropdown, but instead of the Details method in my code behind firing, it hits my Index method. Why is the details method not getting hit on the onchange event? My Details() method in the controller is:

    public ActionResult Details()
    {
        ...   < It never gets here, just goes to the index() method
    }

It's a little frustrating right now since I'm sure it is a simple mistake but not sure what it could be. I looked at the Source of my page and sure enough, the form looks like it should be routing to the Details Action:

<form action="/Portal/Details" method="post" ...

Any help would be appreciated.

+2  A: 

That's because in your onchange handler, you're calling the submit() method on the TheForm form instead of your AJAX form:

    new { onchange = "document.getElementById('TheForm').submit();" })%>
                                               ^^ wrong form ID

Give your AJAX form another ID, and use that instead.

Fyodor Soikin
Thanks Fyodor, I overlooked that, good catch! How do you specify the Form ID in the Ajax.BeginForm()? I don't see that as a parameter...
Mark Kadlec
Use an overload that has the `htmlAttributes` argument, just like you do with `Html.BeginForm()`. If my answer helped you, would you please consider accepting it?
Fyodor Soikin
Fyodor, I found one that had that parameter, and used <% using (Ajax.BeginForm("Details", "", new AjaxOptions { UpdateTargetId = "mpkResults" }, new { id = "SubForm" } )) { %> <%=Html.DropDownList("Docs", (IEnumerable<SelectListItem>)ViewData["Docs"], new { onchange = "document.getElementById('SubForm').submit();" })%>It generates the correct HTML now with a "SubForm", but the Index() method is still being called.I will gladly mark yours as the answer, but would like to see the Details method get executed, that is the confirmation that it works
Mark Kadlec
Ok then, let's go further. Did you include `microsoftajax.js` and `jquery`? Can you give a link to live demo?
Fyodor Soikin
Fyodor, I did include the microsoftajax.js and jquery. I managed to get the Details method to get fired, but only after I removed the parent form 'TheForm'. I made the mistake of nesting the forms and that caused the routing confusion.Thank you very much for your help, hopefully soon I will get the hang of MVC 2.0, right now it is much slower for me over .net forms, but I can see the power of it's control and simplicity.
Mark Kadlec