views:

119

answers:

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?

A: 

Hi, I read the tutorial (specially part 3 serverral times) and I am still not able to fix it. Please can anyon give a hint? thx in advance!

+1  A: 

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?

Kelsey
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...