html.dropdownlistfor

MVC3 DropDownListFor - a simple example?

旧时模样 提交于 2019-11-26 02:07:55
问题 I\'m having trouble with DropDownListFor in my MVC3 app. I was able to use StackOverflow to figure out how to get them to appear on the View, but now I don\'t know how to capture the values in its corresponding properties on the View Model when it\'s submitted. In order to get this to work I had to create an inner class that had an ID and a value property, then I had to use an IEnumerable<Contrib> to satisfy the DropDownListFor parameter requirements. Now, however, how is MVC FW supposed to

How to write a simple Html.DropDownListFor()?

独自空忆成欢 提交于 2019-11-26 01:29:07
问题 In ASP.NET MVC 2, I\'d like to write a very simple dropdown list which gives static options. For example I\'d like to provide choices between \"Red\", \"Blue\", and \"Green\". 回答1: See this MSDN article and an example usage here on Stack Overflow. Let's say that you have the following Linq/POCO class: public class Color { public int ColorId { get; set; } public string Name { get; set; } } And let's say that you have the following model: public class PageModel { public int MyColorId { get; set

Can the ViewBag name be the same as the Model property name in a DropDownList?

半腔热情 提交于 2019-11-25 21:55:36
问题 I am working on an ASP.NET MVC-4 web application. I\'m defining the following inside my action method to build a SelectList : ViewBag.CustomerID = new SelectList(db.CustomerSyncs, \"CustomerID\", \"Name\"); Then I am rendering my DropDownListFor as follow inside my View : @Html.DropDownListFor(model => model.CustomerID, (SelectList)ViewBag.CustomerID, \"please select\") As shown I am naming the ViewBag property to be equal to the Model property name which is CustomerID . From my own testing,