MVC 3 DropDownListFor and the wrong value error
-
07-11-2019 - |
質問
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."
正しい解決策はありません