我有一个连续三个元素的CreateGame视图:一个label ,一个dropdownlist和一个用于输入十进制数字的editbox 。
它是这样的:
<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>基本上我想确定哪个玩家(来自下拉列表)玩游戏并给他一定的评分。
现在我有两种不同的场景。 我可以为评级设置一个整数,一切运行顺利,或者我可以输入一个十进制数字(这就是重点),并在此行中抛出System.InvalidOperationException异常:
@Html.DropDownListFor(model => model.id1, (SelectList)ViewBag.Lista, htmlAttributes: new { @class = "form-control" })说明
具有键“id1”的ViewData项的类型为“System.Int32”,但必须是“IEnumerable”类型。
我知道错误消息在说什么,但我不明白rating属性如何改变编译器在上面一行的方式。 如何能够为评级插入十进制数字?
编辑:
顺便说一句,这就是我在控制器上创建ViewBag.Lista :
var lista = db.Players .Select(p => new SelectListItem { Text = p.name, Value = p.ID.ToString() }).ToList(); ViewBag.Lista = new SelectList(lista, "Value", "Text");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");最满意答案
尝试在你的模型中设置id1 :
public IEnumerable<SelectListItem> Your_name_list {get; set;}并在视图中设置此列表:
@Html.DropDownListFor(model => model.id1, model.Your_name_list, htmlAttributes: new { @class = "form-control" })Try in your model where you have id1 set:
public IEnumerable<SelectListItem> Your_name_list {get; set;}And in the view set this list:
@Html.DropDownListFor(model => model.id1, model.Your_name_list, htmlAttributes: new { @class = "form-control" })更多推荐
发布评论