views:

72

answers:

3

I have a code black 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!

A: 

Have a look at this blog post, it'll help you get a better grasp on how the Ajax.BeginForm actually works. I'd suggest going back to the Microsoft documentation to ensure you have a good grasp on the Ajax library and it's operation.

Lazarus
A: 

ViewData is not the place to pass data back to the server side. Values of html input controls within form tag are conveniently available in action method. You can get these values either from various types of action method arguments (model, formcollection etc).

Here is a link to free asp.net mvc ebook tutorial. Is a good resource for asp.net mvc.

Ricky Supit
A: 

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
        });
    });
});
Johannes Setiabudi