I have a problem with my code snippet inside the MVC 3 with Razor syntax. In my DB table (MySQL), I have a colum (FK) LanguageID [int]. Then, in that View, I want to assign to that column to the DropDownList with the languages list that I generate on the fly:
List<SelectListItem> list = new List<SelectListItem>();
list.Add(new SelectListItem
{
Value = tableRow.ID.ToString()
,
Text = "language"
}
Then, in the View, I connects that list to the Language property:
@Html.DropDownListFor(
model => model.Language,
new SelectList(ViewData["LanguageList"] as List<SelectListItem>,
"Value", "Text")
)
but, when I post the data to the server, it returns with the error result "The value '353' is invalid." where the 353 is the Language ID from the DB. What am I doing wrong ?
Edit
Whle debugging, I’ve noticed the error message within the ModelState: "The parameter conversion from type 'System.String' to type 'FavorytaWeb.Models.BookLanguage' failed because no type converter can convert between these types."
Replace:
with:
You cannot bind the value
353to a complex type which is whatmodel.Languagerepresents. It just doesn’t make sense. You can bind it only to scalar properties.