Asp.net mvc 3 在ASP.NET MVC 3中填充下拉列表
我需要在ASP.NET MVC 3中填充一个下拉列表。我希望得到以下问题的答案:Asp.net mvc 3 在ASP.NET MVC 3中填充下拉列表,asp.net-mvc-3,razor,drop-down-menu,Asp.net Mvc 3,Razor,Drop Down Menu,我需要在ASP.NET MVC 3中填充一个下拉列表。我希望得到以下问题的答案: 我有什么选择。我是说@Html.DropDownList和@Html.DropDownListFor有什么区别。还有其他选择吗 我有以下的类/代码。假设我只需要使用下面的类显示这个下拉列表,那么我在.cshtml中需要什么样的razor代码/HTML语法(我已经包含了我正在尝试的内容) public class SearchCriterion { public int Id { get; set; } p
public class SearchCriterion
{
public int Id { get; set; }
public string Text { get; set; }
public string Value { get; set; }
}
public class SearchCriteriaViewModel
{
public IEnumerable<SelectListItem> SearchCriteria { get; set; }
}
public class HomeController : Controller
{
public ActionResult Index()
{
IList<System.Web.WebPages.Html.SelectListItem> searchCriteriaSelectList =
new List<System.Web.WebPages.Html.SelectListItem>();
SearchCriteriaViewModel model = new SearchCriteriaViewModel();
//Some code to populate searchCriteriaSelectList goes here....
model.SearchCriteria = searchCriteriaSelectList;
return View(model);
}
}
//Code for Index.cshtml
@model SearchCriteriaViewModel
@{
ViewBag.Title = "Index";
}
<p>
@Html.DropDownListFor(model => model.SearchCriteria,
new SelectList(SearchCriteria, "Value", "Text"))
</p>
公共类搜索标准
{
公共int Id{get;set;}
公共字符串文本{get;set;}
公共字符串值{get;set;}
}
公共类SearchCriteriaViewModel
{
公共IEnumerable搜索条件{get;set;}
}
公共类HomeController:控制器
{
公共行动结果索引()
{
IList searchCriteriaSelectList=
新列表();
SearchCriteriaViewModel=新的SearchCriteriaViewModel();
//这里有一些填充searchCriteriaSelectList的代码。。。。
model.SearchCriteria=searchCriteriaSelectList;
返回视图(模型);
}
}
//Index.cshtml的代码
@模型搜索标准视图模型
@{
ViewBag.Title=“Index”;
}
@Html.DropDownListFor(model=>model.SearchCriteria,
新建选择列表(搜索条件、“值”、“文本”))
lambda
=>
的右侧必须是简单类型,而不是像修改代码那样的复杂类型
public class SearchCriteriaViewModel
{
public int Id { get; set; }
public IEnumerable<SearchCriterion> SearchCriteria { get; set; }
}
public class SearchCriterion
{
public string Text { get; set; }
public string Value { get; set; }
}
公共类SearchCriteriaViewModel
{
公共int Id{get;set;}
公共IEnumerable搜索条件{get;set;}
}
公共类搜索标准
{
公共字符串文本{get;set;}
公共字符串值{get;set;}
}
控制器看起来像
public ActionResult Index()
{
//fill the select list
IEnumerable<SearchCriteria> searchCriteriaSelectList =
Enumerable.Range(1,5).Select(x=>new SearchCriteria{
Text= x.ToString(),
Value=ToString(),
});
SearchCriteriaViewModel model = new SearchCriteriaViewModel();
//Some code to populate searchCriteriaSelectList goes here....
model.SearchCriteria = searchCriteriaSelectList;
return View(model);
}
public ActionResult Index()
{
//填写选择列表
IEnumerable searchCriteriaSelectList=
Enumerable.Range(1,5)。选择(x=>new SearchCriteria{
Text=x.ToString(),
Value=ToString(),
});
SearchCriteriaViewModel=新的SearchCriteriaViewModel();
//这里有一些填充searchCriteriaSelectList的代码。。。。
model.SearchCriteria=searchCriteriaSelectList;
返回视图(模型);
}
在视图中
@model SearchCriteriaViewModel
@{
ViewBag.Title = "Index";
}
<p>
@Html.DropDownListFor(model => model.Id,
new SelectList(model.SearchCriteria , "Value", "Text"))
</p>
@model SearchCriteriaViewModel
@{
ViewBag.Title=“Index”;
}
@DropDownListFor(model=>model.Id,
新的选择列表(model.SearchCriteria、“值”、“文本”))
lambda=>
的右侧必须是简单类型,而不是复杂类型修改您的代码
public class SearchCriteriaViewModel
{
public int Id { get; set; }
public IEnumerable<SearchCriterion> SearchCriteria { get; set; }
}
public class SearchCriterion
{
public string Text { get; set; }
public string Value { get; set; }
}
公共类SearchCriteriaViewModel
{
公共int Id{get;set;}
公共IEnumerable搜索条件{get;set;}
}
公共类搜索标准
{
公共字符串文本{get;set;}
公共字符串值{get;set;}
}
控制器看起来像
public ActionResult Index()
{
//fill the select list
IEnumerable<SearchCriteria> searchCriteriaSelectList =
Enumerable.Range(1,5).Select(x=>new SearchCriteria{
Text= x.ToString(),
Value=ToString(),
});
SearchCriteriaViewModel model = new SearchCriteriaViewModel();
//Some code to populate searchCriteriaSelectList goes here....
model.SearchCriteria = searchCriteriaSelectList;
return View(model);
}
public ActionResult Index()
{
//填写选择列表
IEnumerable searchCriteriaSelectList=
Enumerable.Range(1,5)。选择(x=>new SearchCriteria{
Text=x.ToString(),
Value=ToString(),
});
SearchCriteriaViewModel=新的SearchCriteriaViewModel();
//这里有一些填充searchCriteriaSelectList的代码。。。。
model.SearchCriteria=searchCriteriaSelectList;
返回视图(模型);
}
在视图中
@model SearchCriteriaViewModel
@{
ViewBag.Title = "Index";
}
<p>
@Html.DropDownListFor(model => model.Id,
new SelectList(model.SearchCriteria , "Value", "Text"))
</p>
@model SearchCriteriaViewModel
@{
ViewBag.Title=“Index”;
}
@DropDownListFor(model=>model.Id,
新的选择列表(model.SearchCriteria、“值”、“文本”))
在过去3年中广泛使用ASP.NET MVC之后,我更喜欢使用更多的视图数据
将您的[列表项]作为匿名对象传递到Html.EditorFor()
方法中,该对象具有与模型属性相同的属性名
好处是Html.EditorFor()
方法自动使用编辑器模板。
因此,您不需要为下拉列表提供CSS类名。
见下面的比较
//------------------------------
// additionalViewData <=== RECOMMENDED APPROACH
//------------------------------
@Html.EditorFor(
m => m.MyPropertyName,
new { MyPropertyName = Model.ListItemsForMyPropertyName }
)
//------------------------------
// traditional approach requires to pass your own HTML attributes
//------------------------------
@Html.DropDown(
"MyPropertyName",
Model.ListItemsForMyPropertyName,
new Dictionary<string, object> {
{ "class", "myDropDownCssClass" }
}
);
//------------------------------
// DropDownListFor still requires you to pass in your own HTML attributes
//------------------------------
@Html.DropDownListFor(
m => m.MyPropertyName,
Model.ListItemsForMyPropertyName,
new Dictionary<string, object> {
{ "class", "myDropDownCssClass" }
}
);
//------------------------------
//附加ViewData m.MyPropertyName,
新建{MyPropertyName=Model.ListItemsForMyPropertyName}
)
//------------------------------
//传统的方法需要传递自己的HTML属性
//------------------------------
@Html.0下拉列表(
“MyPropertyName”,
Model.ListItemsForMyPropertyName,
新词典{
{“类”,“myDropDownCssClass”}
}
);
//------------------------------
//DropDownListFor仍然要求您传入自己的HTML属性
//------------------------------
@Html.DropDownListFor(
m=>m.MyPropertyName,
Model.ListItemsForMyPropertyName,
新词典{
{“类”,“myDropDownCssClass”}
}
);
如果您想了解更多详细信息,请参阅。在过去3年中广泛使用ASP.NET MVC之后,我更喜欢使用更多的视图数据
将您的[列表项]作为匿名对象传递到Html.EditorFor()
方法中,该对象具有与模型属性相同的属性名
好处是Html.EditorFor()
方法自动使用编辑器模板。
因此,您不需要为下拉列表提供CSS类名。
见下面的比较
//------------------------------
// additionalViewData <=== RECOMMENDED APPROACH
//------------------------------
@Html.EditorFor(
m => m.MyPropertyName,
new { MyPropertyName = Model.ListItemsForMyPropertyName }
)
//------------------------------
// traditional approach requires to pass your own HTML attributes
//------------------------------
@Html.DropDown(
"MyPropertyName",
Model.ListItemsForMyPropertyName,
new Dictionary<string, object> {
{ "class", "myDropDownCssClass" }
}
);
//------------------------------
// DropDownListFor still requires you to pass in your own HTML attributes
//------------------------------
@Html.DropDownListFor(
m => m.MyPropertyName,
Model.ListItemsForMyPropertyName,
new Dictionary<string, object> {
{ "class", "myDropDownCssClass" }
}
);
//------------------------------
//附加ViewData m.MyPropertyName,
新建{MyPropertyName=Model.ListItemsForMyPropertyName}
)
//------------------------------
//传统的方法需要传递自己的HTML属性
//------------------------------
@Html.0下拉列表(
“MyPropertyName”,
Model.ListItemsForMyPropertyName,
新词典{
{“类”,“myDropDownCssClass”}
}
);
//------------------------------
//用于st的下拉列表