I am trying to get a selected item from a dropdown list in the model. But I am always getting null vale for the item.
My model is
public class Configuration
{
public Color BoderColor { get; set; }
}
public class Color
{
public long Id { get; set; }
public string Name { get; set; }
}
And I am binding dropdown list like this
<td>
@Html.DropDownListFor(m => m.BoderColor, new SelectList(ViewBag.Colors, "Id","Name" ))
</td>
ViewBag.Colors is of type IEnumerable<Color>
And this is my action method.
[HttpPost]
public ActionResult Create(Configuration configItem)
{
try
{
var color = configItem.BoderColor;
return RedirectToAction("List");
}
catch
{
return View();
}
}
When I submit my view, I am expecting the selected color should be able to access via ‘BoderColor’ in model object. Can somebody help me on this.
Thanks.
Add an int property in your model representing the selected id for the color :
And then use it in the helper