Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

asp.net mvc - DropDown list onchange event and AJAX in MVC

I have a code block 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 please suggest how can i do this?

thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

$(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
        });
    });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...