4

I'm trying to make use of the onChange event for my dropdownlist in my ASP.NET MVC project, althought it is not working.

The view, containing the form, looks like this:

@using (Ajax.BeginForm("Action", "Controller",
 new AjaxOptions
 {
     HttpMethod = "POST",
     UpdateTargetId = "pages",
     InsertionMode = InsertionMode.Replace
 }))
        {
            @Html.DropDownListFor(m => m.SelectedFrom, Model.ChangeFrom, new { onChange = "this.form.submit();" })                
        }

       <div id="pages"></div>

With the code above, onChange fires when selecting an item in the dropdownlist as expected. Although it's not making use of ajax, it is simply redirecting me to a new page, instead of just updating/filling the "pages-Div".

Although...

If I delete the onChange event for the dropdownlist and add a simple submit button, like this:

 @using (Ajax.BeginForm("SelectedUser", "ReplaceName",
 new AjaxOptions
 {
     HttpMethod = "POST",
     UpdateTargetId = "pages",
     InsertionMode = InsertionMode.Replace
 }))
        {
            @Html.DropDownListFor(m => m.SelectedFrom, Model.ChangeFrom)
            <input type="submit" value="GO" />
        }

it uses Ajax and only reloads the "pages-Div".

Am I missing something?

Regards, Chris

2 Answers 2

2

I got it working by using jQuery instead:

onchange = "$(this.form).submit();"

Is there a better jQuery solution to this.form.submit();?

Sign up to request clarification or add additional context in comments.

Comments

0

The first scenario redirecting you to a new page because you are performing a form.submit(), but are returning false so the action is running to completion. if you change new

{ onChange = "this.form.submit();" } 

to

{ onChange = "this.form.submit(); return false;" }

you might have to write a tiny javascript function to make both calls.

But I don't think you want that because you aren't going to get the div replacement that the Ajax form will do for you. You'd have to wire that up yourself.

The second one stays on the page because that's how Ajax.BeginForm works. It knows that that input is tied to a Ajax form you are submitting and intercepts the call using the values in AjaxOptions.

4 Comments

Not entirely sure I understand what you mean. I thought the this.form.submit() function was the same that is triggered behind the <input type="submit">. Since I want the dropdownlist to trigger a method in a controller if changed, how do I do this for ajax?
are you looking to submit the form or just call some random action on the controller?
Well I want to submit the form since I want the value in the dropdown to be passed to the controller method. But I want the result of the method to return as a partialview, using ajax and only updated relevant div element.
Since you are changing anything on the server side. I would actually go with a GET here and use $(#pages-div).load(). So something like $('#SelectedFrom').change(function() { $(#pages-div).load('<your controller call>') });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.