Asp.net mvc 将DropDownList绑定到MVC视图中
从这里读到: 已在控制器内创建操作Asp.net mvc 将DropDownList绑定到MVC视图中,asp.net-mvc,Asp.net Mvc,从这里读到: 已在控制器内创建操作SelectCategory- public ActionResult SelectCategory() { List<SelectListItem> items = new List<SelectListItem>(); items.Add(new SelectListItem { Text = "Action", Value = "0"}); items.Add(new SelectL
SelectCategory
-
public ActionResult SelectCategory() {
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "Action", Value = "0"});
items.Add(new SelectListItem { Text = "Drama", Value = "1" });
items.Add(new SelectListItem { Text = "Comedy", Value = "2", Selected = true });
ViewBag.MovieType = items;
return View();
}
以类似的方式绑定数据时
@Html.DropDownList("IdList");
我得到以下错误-
没有类型为“IEnumerable”的ViewData项
具有键“IdList”
控制器动作:
public ActionResult SelectId()
{
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "MyId1", Value = "MyId1", Selected=true });
items.Add(new SelectListItem { Text = "MyId2", Value = "MyId2" });
ViewBag.IdList = items;
return View();
}
public ActionResult SelectId()
{
列表项=新列表();
添加(新SelectListItem{Text=“MyId1”,Value=“MyId1”,Selected=true});
添加(新SelectListItem{Text=“MyId2”,Value=“MyId2”});
ViewBag.IdList=项目;
返回视图();
}
我错过了什么?谢谢你的帮助 将列表绑定到下拉列表时,您需要键入cast将其转换为IEnumerable,因为您使用@Html.DropDown控件,该控件不是与Model一起强键入的 鉴于
@Html.DropDownList("MovieType",(IEnumerable<SelectListItem>)ViewBag.MovieType)
@Html.DropDownList(“MovieType”,(IEnumerable)ViewBag.MovieType)
另一方面,如果你把懒汉绑在一起
@Html.DropDownList("IdList",(IEnumerable<SelectListItem>)ViewBag.IdList)
@Html.DropDownList(“IdList”,(IEnumerable)ViewBag.IdList)
使用@Html.DropDownList(“MovieType”)
时,您已经设置了ViewBag.MovieType
=>下拉列表将使用此值。编写@Html.DropDownList(“IdList”)
时,助手在ViewBag中找不到相应的IdList
属性,并抛出错误,因为它不知道从何处绑定数据
或者,如果要更改下拉列表的名称,可以使用以下选项:
@Html.DropDownList("SelectedMovieType", (IEnumerable<SelectListItem>)ViewBag.MovieType)
然后让控制器操作填充此视图模型并将其传递给视图:
public ActionResult SelectId()
{
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "MyId1", Value = "MyId1", Selected=true });
items.Add(new SelectListItem { Text = "MyId2", Value = "MyId2" });
var model = new MyViewModel
{
MovieTypes = items
};
return View(model);
}
数据绑定将数据绑定到模型的一种简单方法:不要使用razor进行下拉。手动绑定,使用FormCollection对象获取 控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Sorun sorun,FormCollection fc)
{
//Define independently as form collection
sorun.GonderilecekBirim = fc["GonderilecekBirim"];
sorun.IlkSorulmaTarihi = DateTime.Now;
if (ModelState.IsValid)
{
db.Soruns.Add(sorun);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(sorun);
}
看法
@Html.LabelFor(model=>model.GonderilecekBirim,htmlAttributes:new{@class=“control label col-md-2”})
萨顿阿尔玛酒店
木贼
@*@Html.ValidationMessageFor(model=>model.GonderilecekBirim,“,new{@class=“text danger”})*@
一切似乎都很好。也许你应该试试@Html.DropDownList(ViewBag.IdList);DropDownList有8(八)个重载,允许您指定所有内容,包括要在选项中显示的列表。请在此处浏览Html.DropDownList的文档试用此Html.DropDownList(“anyname”,ViewBag.IdList)-此构造函数接受两个参数-name和要显示的列表hi Shivkumar,上面的代码引发错误:没有类型为“IEnumerable”且键为“IdList”的ViewData项。
有什么建议吗?@autrevo您能给我看一些错误快照吗,因为我无法在此处复制它
public ActionResult SelectId()
{
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "MyId1", Value = "MyId1", Selected=true });
items.Add(new SelectListItem { Text = "MyId2", Value = "MyId2" });
var model = new MyViewModel
{
MovieTypes = items
};
return View(model);
}
@model MyViewModel
@Html.DropDownListFor(x => x.SelectedMovieType, Model.MovieTypes)
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Sorun sorun,FormCollection fc)
{
//Define independently as form collection
sorun.GonderilecekBirim = fc["GonderilecekBirim"];
sorun.IlkSorulmaTarihi = DateTime.Now;
if (ModelState.IsValid)
{
db.Soruns.Add(sorun);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(sorun);
}
@Html.LabelFor(model => model.GonderilecekBirim, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<!-- Define as raw html -->
<select id="GonderilecekBirim" name="GonderilecekBirim">
<option value="4">Satın Alma</option>
<option value="5">Muhasebe</option>
</select>
@*@Html.ValidationMessageFor(model => model.GonderilecekBirim, "", new { @class = "text-danger" })*@
</div>