DropDownLists is probably my favorite part of working with an MVC map. I have several drop-down lists in my form whose selected values I need to pass to ActionResult, which takes the model as its parameter.
The markup is as follows:
<div class="editor-label">
@Html.LabelFor(model => model.FileType)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.FileType.Variety, (SelectList)ViewBag.FileTypes)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Status)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Status.Status, (SelectList)ViewBag.Status)
</div>
And my controller action looks like this:
[HttpPost]
public ActionResult Create(int reviewid, ReviewedFile file)
{
if (ModelState.IsValid)
{
UpdateModel(file);
}
return RedirectToAction("Files", "Reviews", new { reviewid = reviewid, id = file.ReviewedFileId });
}
This should be good and good, except that the values from the drop-down lists are published as null. When I look further into ModelState errors, the cause is defined as:
The conversion of parameters from the type 'System.String' to the input of type 'PeerCodeReview.Models.OutcomeStatus' failed because the type converter cannot convert between these types.
, . , : ?
, , FormCollection, , .