Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 将viewbag传递给@Dropdownlist_Asp.net Mvc_Razor - Fatal编程技术网

Asp.net mvc 将viewbag传递给@Dropdownlist

Asp.net mvc 将viewbag传递给@Dropdownlist,asp.net-mvc,razor,Asp.net Mvc,Razor,我正在尝试将查看包传递给DropDownList,但不起作用 @Html.DropDownList("TransporteId", ViewBag.BanhoTosaId, htmlAttributes: new { @class = "form-control" }) 下拉列表错误 @Html.DropDownList("TransporteId", ViewBag.BanhoTosaId, htmlAttributes: new { @class = "form-control" })

我正在尝试将查看包传递给DropDownList,但不起作用

 @Html.DropDownList("TransporteId", ViewBag.BanhoTosaId, htmlAttributes: new { @class = "form-control" })
下拉列表错误

 @Html.DropDownList("TransporteId", ViewBag.BanhoTosaId, htmlAttributes: new { @class = "form-control" })

 @Html.DropDownList("TransporteId", ViewBag.BanhoTosaId, htmlAttributes: new { @class = "form-control" })
看法

 @Html.DropDownList("TransporteId", ViewBag.BanhoTosaId, htmlAttributes: new { @class = "form-control" })
如果我让下拉列表以这种方式显示@Html.DropDownList(“TransporteId”,null,htm。。。。 如果在控制器中抛出else方法,则返回if

 @Html.DropDownList("TransporteId", ViewBag.BanhoTosaId, htmlAttributes: new { @class = "form-control" })
如何解决此问题?

替换

 @Html.DropDownList("TransporteId", ViewBag.BanhoTosaId, htmlAttributes: new { @class = "form-control" })

 @Html.DropDownList("TransporteId", ViewBag.BanhoTosaId, htmlAttributes: new { @class = "form-control" })
@Html.DropDownList("TransporteId", (IEnumerable<SelectListItem>)ViewBag.BanhoTosaId,null, new { @class = "form-control" })
@Html.DropDownList(“TransporteId”,(IEnumerable)ViewBag.BanhoTosaId,null,new{@class=“form control”})

您需要强制转换ViewBag.BanhoTosaId:

 @Html.DropDownList("TransporteId", ViewBag.BanhoTosaId, htmlAttributes: new { @class = "form-control" })
 @Html.DropDownList("TransporteId", (List<SelectList>) ViewBag.BanhoTosaId, htmlAttributes: new { @class = "form-control" })

 @Html.DropDownList("TransporteId", ViewBag.BanhoTosaId, htmlAttributes: new { @class = "form-control" })
然后在razor视图中添加:

 @Html.DropDownList("TransporteId", ViewBag.BanhoTosaId, htmlAttributes: new { @class = "form-control" })
@Html.DropDownList("TransporteId", new SelectList(ViewBag.TransporteId , "TransporteId", "Tipo"))

您首先需要从数据中创建一个列表,您希望通过它填充列表,如下所示:

 @Html.DropDownList("TransporteId", ViewBag.BanhoTosaId, htmlAttributes: new { @class = "form-control" })
List<Category> CatList = db.Categories.ToList();
之后,您将查看并编写:

 @Html.DropDownList("TransporteId", ViewBag.BanhoTosaId, htmlAttributes: new { @class = "form-control" })

@Html.DropDownListFor(model=>model.CategoryID,ViewBag.CategoriesList作为SelectList,
“选择类别”,新建{@class=“form control”})
@Html.ValidationMessageFor(model=>model.CategoryID,“,new{@class=“text danger”})

你到底想在这里做什么?如果(ViewBag.BanhoTosaId!=null和&ViewBag.SelectedValue!=null)既然你刚刚设置了它们,而且它们永远都不能为
null
首先你需要在你的方法中强制转换SelectList-
@Html.DropDownList(“TransporteId”,(IEnumerable)ViewBag.BanhoTosaId
..}`但这没有任何意义,因为您试图绑定到
TransporteId
,而不是
BanhoTosaId
。但这也不会起作用(从阅读开始理解。停止使用视图包并使用视图模型)