Reputation: 2673
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.
Upvotes: 2
Views: 765
Reputation: 2775
Add an int property in your model representing the selected id for the color :
public class Configuration
{
public Color BoderColor { get; set; }
public int BoderColorId { get; set; }
}
And then use it in the helper
<td>
@Html.DropDownListFor(m => m.BoderColorId, new SelectList(ViewBag.Colors, "Id","Name" ))
</td>
Upvotes: 1