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?

Community
  • 1
  • 1
user415876
  • 327
  • 4
  • 19
  • 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. http://www.asp.net/mvc/tutorials/mvc-music-store-part-2 – bzlm Aug 10 '10 at 08:10
  • Thx. Ok. True, a look to the tutorials will help ... sorry for been so stupid to post a question like that. – user415876 Aug 10 '10 at 08:18

1 Answers1

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?

Community
  • 1
  • 1
Kelsey
  • 47,246
  • 16
  • 124
  • 162
  • 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? – user415876 Aug 13 '10 at 04:00
  • 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 __checkbox__ { get; set;}" to handle all the needs of a view. – user415876 Aug 13 '10 at 04:10
  • 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... – user415876 Aug 13 '10 at 04:12