Dropdown list onchange event and AJAX in MVC

I have black code in my MVC view as follows:

<%using (Ajax.BeginForm("MyAction", new { action = "MyAction", controller = "Home", id = ViewData["selected"].ToString() }, new AjaxOptions { UpdateTargetId = "Div1" }))
     { %>
          <%=Html.DropDownList("ddl", ViewData["MyList"] as SelectList, new { onchange = "this.form.submit()" })%>
                 <%} %>

I want to set the value of ViewData ["selected"] so that I can send it to the desired action. Can anyone suggest how I can do this?

thank!

+3
source share
4 answers

Instead of using a form, why not use the jQuery onChange event on the drop-down list?

$(document).ready(function() {
    $("#ddl").change(function() {
        var strSelected = "";
        $("#ddl option:selected").each(function() {
            strSelected += $(this)[0].value;
        });
        var url = "/Home/MyAction/" + strSelected;

        $.post(url, function(data) {
            // do something if necessary
        });
    });
});
+11
source

ViewData . HTML- . (model, formcollection ..).

asp.net mvc. asp.net mvc.

+2

, , Ajax.BeginForm. Microsoft, , Ajax .

+1

The solution found in this post is just a little chnge

Yes, this is right - only the change is replaced:

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

with:

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

0
source

Source: https://habr.com/ru/post/1732174/


All Articles