I have looked at all the examples related to this but have not been able to solve my issue.
I am creating a dropdownlist in asp .net mvc3.
I have a repository which returns:
public IEnumerable<SelectListItem> GetPropertyTypeSelectList()
{
var propertyTypes = from p in db.PropertyType
orderby p.PropertyTypeDescription
select new SelectListItem
{
Text = p.PropertyTypeDescription,
Value = p.PropertyTypeId.ToString()
};
return propertyTypes;
}
My viewmodel looks like this:
public class AddPropertyViewModel
{
public Property Property { get; set; }
public IEnumerable<SelectListItem> PropertyTypes { get; set; }
public IEnumerable<SelectListItem> FurnishedTypes { get; set; }
}
My controller for "create" action for HttpGet looks like this:
public ActionResult AddProperty()
{
AddPropertyViewModel viewModel = new AddPropertyViewModel
{
PropertyTypes = websiterepository.GetPropertyTypeSelectList()
};
return View(viewModel);
}
and the view is like this:
<div class="editor-label">
@Html.LabelFor(model => model.Property.PropertyType)
@Html.DropDownListFor(model => model.Property.PropertyType, Model.PropertyTypes)
</div>
I am getting the error above. From what I have read it looks like ToString() is causing the problem. But I am not sure how to correct it.
Thanks.