C# 使用MVC4中的HTML帮助程序选择下拉列表
我需要在html助手中选择下拉列表。我使用ViewData在IEnumerable中传递数据,请有人帮助我如何使用值选择它 我的代码是C# 使用MVC4中的HTML帮助程序选择下拉列表,c#,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 4,我需要在html助手中选择下拉列表。我使用ViewData在IEnumerable中传递数据,请有人帮助我如何使用值选择它 我的代码是 @Html.DropDownList("Brand", ViewData["Brand"] as IEnumerable<SelectListItem>, item.BrandMaster, new { @class = "form-control Repeat TableMust"}) @Html.DropDownList(“Brand”,Vie
@Html.DropDownList("Brand", ViewData["Brand"] as IEnumerable<SelectListItem>, item.BrandMaster, new { @class = "form-control Repeat TableMust"})
@Html.DropDownList(“Brand”,ViewData[“Brand”]作为IEnumerable,item.BrandMaster,新{@class=“form control Repeat TableMust”})
这里
ViewData[“Brand”]
是我的下拉列表item.BrandMaster
值但问题是它显示文本,但不包含值。值为“”空,如何在html帮助程序中选择它?如果绑定到的属性名为
BrandMaster
,则应为
@Html.DropDownListFor(m => m.BrandMaster, ViewData["Brand"] as IEnumerable<SelectListItem>, new { @class = "form-control Repeat TableMust"})
MyModel.cshtml(在Views/Shared/EditorTemplates
@model MyModel
@Html.DropDownListFor(m => m.BrandMaster, ViewData["Brand"] as IEnumerable<SelectListItem>, new { @class = "form-control Repeat TableMust"})
@Html.TextBoxFor(m => m.AnotherProperty)
@model-MyModel
@DropDownListFor(m=>m.BrandMaster,ViewData[“Brand”]作为IEnumerable,新的{@class=“form control Repeat TableMust”})
@Html.TextBoxFor(m=>m.AnotherProperty)
主视图
@model IEnumerable<MyModel>
@using (Html.BeginForm())
{
@Html.EditorFor(m => m, new { Brand = ViewData["Brand"])
....
}
@model IEnumerable
@使用(Html.BeginForm())
{
@EditorFor(m=>m,new{Brand=ViewData[“Brand”])
....
}
在构建下拉列表时,可以使用SelectListItem的selected
属性选择所选项目
myModelCollection = SomeService.GetModels();
var dropdown = myModelCollection.Select(s => new SelectListItem {Value = s.Id, Name = s.Name, Selected = s.Id == YourSelectedValue} ).ToList();
如果您的ViewData[“Brand”]
包含一个对象IEnumerable
,我想您已经使用了它的构造函数或更可能的对象初始值设定项,例如:
List<SelectListItem> brand = new List<SelectListItem>()
{
new { Value = 1 , Text = "something 1" },
new { Value = 2 , Text = "something 2" },
new { Value = 3 , Text = "something 3" },
};
ViewData["Brand"] = brand;
您也可以尝试SelectList
容器来“填充”DropDownList
帮助程序。只需发送要在ViewData等中查看的对象列表,并将其用作第二个参数。然后分别提供值和文本作为第三个和第四个参数。最后一个参数对应于列表中所选项目的值
@Html.DropDownList("Brand", new SelectList(
Model.ListOfYourObjects,
"<<< Property name as value in the Object, e.g. ID",
"<<< Property name as text in the Object, e.g. Name/Title/etc.",
<<< value of thecurrent ID >>>));
然后
ViewData[“Cities”]=dbContext.Cities.ToList();
//或
ViewData[“Cities”]=新建列表();//填写此列表
从某种角度来看:
@model List<City>
// ...
@Html.DropDownList(
"Brand",
new SelectList(
Model, "ID", "Name",
<<< value of ID to be selected or null>>>
)
);
@型号列表
// ...
@Html.DropDownList(
“品牌”,
新选择列表(
型号,“ID”,“名称”,
>
)
);
ID
和Name
是类City
中的属性名称,这对于SelectList
的工作非常重要
我无法尝试此代码,但这会给您一些帮助。希望它能有所帮助。不,我不想选择默认值,我想选择项。BrandMaster值,它会做什么!阅读我的答案如果它是IEnumberable,那么如何选择它当您需要使用自定义
编辑器或模板
。我将很快更新答案(无论如何,您都无法用问题中的代码发回您的收藏)@Dinesh,另请参阅有关在收藏中使用DropDownListFor()
的更详细说明这应该可以@Html.DropDownList(“Brand”,new SelectList((System.Collections.IEnumerable)ViewData[“Brand”],“Id”,“Name”))what is id and what is nameid可以是您的值,Name可以是您的文本。假设Brand表有BrandID和BrandName id=BrandID和Text=BrandName@Vivekh,这不绑定到属性BrandMaster
是的,它不工作
@Html.DropDownList("Brand", new SelectList(
Model.ListOfYourObjects,
"<<< Property name as value in the Object, e.g. ID",
"<<< Property name as text in the Object, e.g. Name/Title/etc.",
<<< value of thecurrent ID >>>));
public class City
{
public int ID { get; set; }
public string Name { get; set; }
}
ViewData["Cities"] = dbContext.Cities.ToList();
//or
ViewData["Cities"] = new List<City>(); // fill this list
@model List<City>
// ...
@Html.DropDownList(
"Brand",
new SelectList(
Model, "ID", "Name",
<<< value of ID to be selected or null>>>
)
);