我正在使用@html.EditorFor
在编辑模式下渲染我的模型,并且不会渲染下拉列表。
这是我的ViewModel:
public class RiskAutoViewModel
{
public RiskAutoViewModel()
{
VehicleMakeList = new SelectList(new List<VehicleMake>() { new VehicleMake() { Id = 1, Name = "Renault" }, new VehicleMake() { Id = 2, Name = "Peugeot" } });
}
public int NoClaimsDegree { get; set; }
public int VehicleValue { get; set; }
public int EngineCapacity { get; set; }
public int VehicleMake { get; set; }
public SelectList VehicleMakeList { get; set; }
}
VehicleMake
呈现为文本框,VehicleMakeList
根本不会呈现。我想要的是渲染一个包含VehicleMake
列表的下拉列表,并将其值设置为VehicleMake
的值。
保存模型时,应将VehicleMake设置为列表中所选项目的值。
我该怎么做?
修改
由于我无法在下面的评论框中输入任何代码,我会在此处写下一个跟进。
我最终创建了一个EditorTemplate,例如:
<div class="editor-label">
@Html.LabelFor(model => model.VehicleMakeList)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.VehicleMake, Model.VehicleMakeList)
@Html.ValidationMessageFor(model => model.VehicleMake)
</div>
现在我的ViewModel看起来像这样:
[Required]
[ScaffoldColumn(false)]
public int VehicleMake { get; set; }
[Display(Name = "Marque", Prompt = "Marque", Description = "Renseigne la marque du véhicule")]
public SelectList VehicleMakeList { get; set; }
现在这引出了另一个问题(也许我应该在另一个问题中),但实际上我在该视图中有两个下拉菜单。第二个下拉列表中的项目基本上是动态的,它们取决于第一个下拉列表中选择的项目。使用AJAX很容易做到这一点但是MVC让我迷失了。人们通常如何做到这一点?
答案 0 :(得分:10)
不幸的是,模板编辑器中没有内置的下拉列表支持。您可以编写自己的编辑器模板,也可以在视图中使用html辅助方法@Html.DropDownListFor()
。
Darin Dimitrov对this question的回答可以引导您完成为下拉列表构建编辑器模板的过程,如果您愿意的话。
实现这一目标的最快方法是在您的视图中执行此操作:
@Html.EditorFor(model => model.NoClaimsDegree)
@Html.EditorFor( model => model.VehicleValue )
@Html.EditorFor( model => model.EngineCapacity )
@Html.DropDownListFor( model => model.VehicleMake, Model.VehicleMakeList, "Select a make" )
答案 1 :(得分:8)
我认为下拉列表的模型应该是:
public List<System.Web.Mvc.SelectListItem> VehicleMakeList {get; set;}
并初始化为:
VehicleMakeList = new List<System.Web.Mvc.SelectListItem>()
{
new SelectListItem { Value = "1", Text = "Renault" },
new SelectListItem { Value = "2", Text = "Peugeot" }
};
或使用数据源:
VehicleMakeList = db.VehicleMakers /*EF, LINQ2SQL, ADO.NET or any supported external source*/
.Select(v=> new SelectListItem { Text = v.Name, Value = v.Id})
.ToList();
查看:
@Html.DropDownListFor(model => model.VehicleMake, Model.VehicleMakeList)
答案 2 :(得分:3)
绑定下拉列表在MVC中非常棘手 您可以在控制器中使用此功能将所有城市列表放入viewBag
创建
ViewBag.CityId = new SelectList(db.Cities, "ID", "Name");
user.CityID如果您在编辑中,那么在编辑时选择城市
ViewBag.CityId = new SelectList(db.Cities, "ID", "Name", user.CityID);
在你的视图中只是做这个技巧
@Html.DropDownList("CityId", "Select")
这是我所知道的最简单的方式......
答案 3 :(得分:0)
绑定下拉列表很容易。代码如下
public ActionResult BindWithModel()
{
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem
{ Text = "Select Category", Value = "0",
Selected = true });
items.Add(new SelectListItem
{ Text = "Beverages", Value = "1" });
items.Add(new SelectListItem
{ Text = "Condiments", Value = "2" });
items.Add(new SelectListItem
{ Text = "Confections", Value = "3" });
items.Add(new SelectListItem
{ Text = "Dairy Products", Value = "4" });
items.Add(new SelectListItem
{ Text = "Grains/Cereals", Value = "5" });
items.Add(new SelectListItem
{ Text = "Meat/Poultry", Value = "6" });
items.Add(new SelectListItem
{ Text = "Produce", Value = "7" });
items.Add(new SelectListItem
{ Text = "Seafood", Value = "8" });
var model = new CategoryModel()
{
lstCategory = items,
selected = 1
};
return View(model);
}
@model BindMvcDropdownList.Models.CategoryModel
<h1>Bind MVC DropDownList with Model</h1>
@Html.DropDownListFor(x => x.selected, Model.lstCategory)
代码来自http://dotnetmentors.com/mvc/how-to-bind-dropdownlist-in-asp-net-mvc-application.aspx