views:

790

answers:

3

Code:

<% using (Ajax.BeginForm("GetResourcesByProject", "CreateRequest", new AjaxOptions { UpdateTargetId = "ResourceListDiv"}))
{
 Response.Write(Html.DropDownList("SelectProject", Model.ProjectList, "Select Project", new { onchange = "this.form.submit();" }));
} %>

When I run the page I get the correct controller action to trigger with the right data in the form collection:

public ActionResult GetResourcesByProject(FormCollection formCollection)

{ var resourceModels = (from project in POTSModel.ProjectList where project.Id == Convert.ToInt32(formCollection["SelectProject"]) select project).First().Resources;

return PartialView("ResourceList", resourceModels); }

It works fine from an Ajax.ActionLink like this:

<%= Ajax.ActionLink("Select", "GetResourcesByProject", "CreateRequest", new { projectId = item.Id }, new AjaxOptions { UpdateTargetId = "ResourceListDiv" })%>

When the post happens I'm navigated to a new page instead of staying on the existing page and updating the contents of the div.

Thanks.

A: 

Do you have an element with this id : ResourceListDiv ?

Gregoire
Nope, it's empty.
Tyler
Sorry read it wrong, yes I have that element. This worked before when using an Ajax.ActionLink.
Tyler
+1  A: 

submit() probably don't trigger Ajax.BeginForm, and so it is processed as usual post. See this for example: http://stackoverflow.com/questions/265293/additional-jquery-events-submitting-my-ajax-beginform. Alternatively add submit button (maybe hidden) and call its .click().

queen3
The hidden submit button works perfectly: <% using (Ajax.BeginForm("GetResourcesByProject", "CreateRequest", new AjaxOptions { UpdateTargetId = "ResourceListDiv" })) { Response.Write(Html.DropDownList("SelectProject", Model.ProjectList, "Select Project", new { onchange = "document.getElementById('projectSubmit').click();" })); %> <input type="submit" name="projectSubmit" style="visibility:hidden" /> <% } %>A bit ugly and cludgy but it works. Bummer that the normal form.submit() doesn't hit the ajax form. Thanks for the help.
Tyler
A: 

Does it works with Internet explorer 7. I have some issue with IE7 in cascading DropDownList. The Ajax.BeginForm doesn't retrieve form (Request.Form["myIdForm"] is blank) Value in IE7, in all others web browser it works (including IE8)!

            <% using (Ajax.BeginForm("profileChanged", "profiles", new AjaxOptions() { UpdateTargetId = "customer", OnComplete = "SetHiddenProfile" }, new { @class = "filtersForm" }))
          {   %>                           
        <p id="customer"> 
            <% Html.RenderPartial("FilterContracts"); %>
        </p>
        <%} %>

I call the database to populate dropDown in profileChanged action and return a partial view ("FilterContracts").

Fctyler