2

And again a newbie question. ;-)

I am setting my View Model based on this very helpfull post:

public class My_IndexModel
{
   ...
   public IEnumerable<SelectListItem> My_DropDownList { get; set; }   
   ...
}

Please note that I use

IEnumerable<SelectListItem>

to be able to set the Selected Property.

The dropdown list values are set in the controller:

public ActionResult Index()
{
     ...
     var DropDownList_Values = from value in My_DB.Value
                               select new SelectListItem
                               {
                                   Selected = (value.IsDefault == 1),
                                   Text = value.Value1,
                                   Value = value.Value1
                                };

     ...

     var viewModel = new My_IndexModel
     {  ...
        My_DropDownList = DropDownList_Values.ToList(), 
        ...
     }

     ...

     return View(viewModel);
}

Means my ViewData model contains (in my case) a dropdownlist.

Also the dropdownlist is in my site (*.aspx) shown and look so:

<%: Html.DropDownList("MyDropDownListField", 
                      new SelectList(Model.My_DropDownList as IEnumerable, 
                                     "Value",
                                     "Text", 
                                     Model.My_DropDownList
                                    )
                     )%>

No problem up to this point.

On the website (*.aspx) I have a simple "submit" button which returns all datas. I fetch the submit event here:

[HttpPost]
public ActionResult Index(My_IndexModel model)
{
    if (ModelState.IsValid)
    {
         ... model.My_DropDownList ...
    }
}

But the DropDownList is empty!

What must be done to get the selected dropdownlist value in the [HttpPost] method?

2
  • I can heartily recommend going through at least one of the official ASP.NET MVC tutorials - preferably one with a DropDownList in it. :) They're very good at giving you a quick but firm grasp of Views, Actions and passing data between the two. asp.net/mvc/tutorials/mvc-music-store-part-2 Commented Aug 10, 2010 at 8:10
  • Thx. Ok. True, a look to the tutorials will help ... sorry for been so stupid to post a question like that. Commented Aug 10, 2010 at 8:18

1 Answer 1

1

You need to add a property with the same name as the DropDownList to the model you are using in the controller that is recieving the post. If the names match up, the framework will put the selected value into the matching model property.

You should look at using Html.DropDownListFor helper.

For more information see this question I posted a while back when I had issues figuring out the best way to implement a DropDownList in MVC:

Best way of implementing DropDownList in ASP.NET MVC 2?

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

3 Comments

Thank you very much! This was really helpfull! But I have still problems to understand/accept(!) that the returned value of the ViewModel will be passed into the Model just by the fact that both classes have a property with the same name! Somehow strange databinding, isnt it?
Finally means this behaviour that we havent THREE "classes" like MVC = Model, View and Controller. No, we have FOUR: Model, View, Controller and the common ViewModel. Example Checkbox: The Model needs just a bool to carry the information "is selected" or "is not selected" (public bool checkbox { get; set;}), but the ViewModel contains a property like "public List<SelectListItem> checkbox { get; set;}" to handle all the needs of a view.
and its getting really strange (for me) at this point: when both properties have exactly the same NAME (like checkbox in our example) is the returned value passed from the ViewModel to the Model...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.