I have my CreateGame
View with three elements in a row: a label
, a dropdownlist
and an editbox
to put a decimal number.
It goes like this:
<div class="form-group">
<div class="col-md-1.5">
@Html.LabelFor(model => model.id1, htmlAttributes: new { @class = "control-label col-md-2" })
</div>
<div class="col-md-2">
@Html.DropDownListFor(model => model.id1, (SelectList)ViewBag.Lista, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.id1, "", new { @class = "text-danger" })
</div>
<div class="col-md-1">
@Html.EditorFor(model => model.rating_1, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.rating_1, "", new { @class = "text-danger" })
</div>
</div>
Basically I want to identify which player (from the dropdownlist) played the game and to assign him a certain rating.
Now I have two different scenarios. I can either put an integer number for the rating and everything runs smoothly or I can put a decimal number (that's the point) and a System.InvalidOperationException
exception is thrown in this line:
@Html.DropDownListFor(model => model.id1, (SelectList)ViewBag.Lista, htmlAttributes: new { @class = "form-control" })
stating
The ViewData item that has the key 'id1' is of type 'System.Int32' but must be of type 'IEnumerable'.
I know what the error message is saying, but I don't understand how can the rating
property changes the way the compiler looks at a line above. How can I be able to insert a decimal number for the rating?
EDIT:
Btw, this is how I create my ViewBag.Lista
on my controller:
var lista = db.Players
.Select(p => new SelectListItem
{
Text = p.name,
Value = p.ID.ToString()
}).ToList();
ViewBag.Lista = new SelectList(lista, "Value", "Text");