asp.net-mvc 将 DropDownList 绑定到 MVC 视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12985088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Binding DropDownList into MVC View
提问by Abhijeet
Reading from here: ASP.NET MVC
从这里阅读:ASP.NET MVC
Action SelectCategoryhas been created inside controller -
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();
}
I am not able to understand binding of Data in following line.
我无法理解以下行中的数据绑定。
@Html.DropDownList("MovieType")
While binding data in similar way,
在以类似方式绑定数据的同时,
@Html.DropDownList("IdList");
I obtain following error-
我得到以下错误-
There is no ViewData item of type 'IEnumerable' that has the key 'IdList'.
没有具有键“IdList”的“IEnumerable”类型的 ViewData 项。
Controller Action:
控制器动作:
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();
}
What am I missing ? Thank you for your help !
我错过了什么?感谢您的帮助 !
回答by Darin Dimitrov
You have set ViewBag.MovieType=> when you use @Html.DropDownList("MovieType")the dropdown will use this value. When you write @Html.DropDownList("IdList"), the helper doesn't find a corresponding IdListproperty in ViewBag and throws an error because it doesn't know from where to bind the data.
您已设置ViewBag.MovieType=> 当您使用@Html.DropDownList("MovieType")下拉列表时将使用此值。当你写的时候@Html.DropDownList("IdList"),helper 没有IdList在 ViewBag 中找到对应的属性并抛出一个错误,因为它不知道从哪里绑定数据。
Alternatively if you want to change the name of the dropdown you could use the following:
或者,如果您想更改下拉菜单的名称,您可以使用以下命令:
@Html.DropDownList("SelectedMovieType", (IEnumerable<SelectListItem>)ViewBag.MovieType)
and your POST action will have a SelectedMovieTypeparameter to retrieve the selected value.
并且您的 POST 操作将有一个SelectedMovieType参数来检索所选值。
But I would avoid ViewBag. Defining a view model is better:
但我会避免使用 ViewBag。定义一个视图模型更好:
public class MyViewModel
{
public string SelectedMovieType { get; set; }
public IEnumerable<SelectListItem> MovieTypes { get; set; }
}
and then have your controller action populate this view model and pass it to the view:
然后让您的控制器操作填充此视图模型并将其传递给视图:
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);
}
and in your strongly typed view:
并在您的强类型视图中:
@model MyViewModel
@Html.DropDownListFor(x => x.SelectedMovieType, Model.MovieTypes)
回答by Shivkumar
while binding you list to DropDown you need to type cast it to IEnumerable as you use @Html.DropDown control which is not strongly type with Model
在将您的列表绑定到 DropDown 时,您需要将它的类型转换为 IEnumerable,因为您使用 @Html.DropDown 控件,该控件不是模型的强类型
In View
在视图中
@Html.DropDownList("MovieType",(IEnumerable<SelectListItem>)ViewBag.MovieType)
and for another way if you bind IdList then
另一种方式,如果你绑定 IdList 那么
@Html.DropDownList("IdList",(IEnumerable<SelectListItem>)ViewBag.IdList)
回答by Suat Atan PhD
A painless way for data binding to bind data to the model: Do not use the razor for Dropdown.Bind it manually, get it with FormCollection object.
数据绑定将数据绑定到模型的一种轻松方式:不要使用 razor 进行 Dropdown。手动绑定,使用 FormCollection 对象获取。
Controller:
控制器:
[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);
}
View
看法
@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>

