如何在asp.net mvc中使下拉列表显示选定的值?
我有一个带有 Html.DropDownList 的编辑页面......我无法显示下拉列表值,它总是显示为 Select
而我想让下拉列表显示基于模型值说Model.Mes_Id
...任何建议如何完成...
<p>
<label for="MeasurementTypeId">MeasurementType:</label>
<%= Html.DropDownList("MeasurementType", // what should i give here?)%>
<%= Html.ValidationMessage("MeasurementTypeId", "*") %>
</p>
编辑:它有列表项,但我想显示在编辑视图...
public ActionResult Edit(int id)
{
var mesurementTypes = consRepository.FindAllMeasurements();
ViewData["MeasurementType"] = mesurementTypes;
var material = consRepository.GetMaterial(id);
return View("Edit", material);
}
我的存储库方法,
public IEnumerable<SelectListItem> FindAllMeasurements()
{
var mesurements = from mt in db.MeasurementTypes
select new SelectListItem
{
Value = mt.Id.ToString(),
Text= mt.Name
};
return mesurements;
}
I have an edit page with a Html.DropDownList in it....I cant show the dropdownlist value it always shows up with Select
instead i want to make the dropdown show an item as selected based on a model value say Model.Mes_Id
... Any suggestion how it can be done...
<p>
<label for="MeasurementTypeId">MeasurementType:</label>
<%= Html.DropDownList("MeasurementType", // what should i give here?)%>
<%= Html.ValidationMessage("MeasurementTypeId", "*") %>
</p>
EDIT: It has the list items but i want to show a value selected in the edit view...
public ActionResult Edit(int id)
{
var mesurementTypes = consRepository.FindAllMeasurements();
ViewData["MeasurementType"] = mesurementTypes;
var material = consRepository.GetMaterial(id);
return View("Edit", material);
}
My repository method,
public IEnumerable<SelectListItem> FindAllMeasurements()
{
var mesurements = from mt in db.MeasurementTypes
select new SelectListItem
{
Value = mt.Id.ToString(),
Text= mt.Name
};
return mesurements;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
创建
IEnumerable
时设置所选项目。就我个人而言,我会为表单创建一个专门的视图模型,但根据您的代码,执行以下操作:
然后将存储库方法更改为:
HTHs,
查尔斯
Set the selected item when you create the
IEnumerable<SelectListItem>
.Personally I would create a specialized viewmodel for the form but going by your code, do something like:
Then change your repository method to something like:
HTHs,
Charles
看看这篇博客文章。
http://weblogs.asp.net/ashicmahtab/archive/2009/03/27/asp-net-mvc-html-dropdownlist-and-selected-value.aspx
基本上,您需要转换您的
measurementTypes
列表/可枚举到SelectList
或IEnumerable
。如果可能的话,我建议升级到 ASP.NET MVC2 并使用
Html.DropDownListFor()
Have a look at this blog entry.
http://weblogs.asp.net/ashicmahtab/archive/2009/03/27/asp-net-mvc-html-dropdownlist-and-selected-value.aspx
Basically, you need to convert your
mesurementTypes
list/enumerable into aSelectList
orIEnumerable<SelectListItem>
.I would recommend, if possible, upgrading to ASP.NET MVC2 and using
Html.DropDownListFor()
您应该返回一个可以指定所选项目的 SelectionList。
如何使用 ASP.NET MVC 创建 DropDownList
You should be returning a SelectionList which can specify a selected item.
How to create a DropDownList with ASP.NET MVC
假设 Model.Mes_Id 包含所选值,您可以执行以下操作
Assuming that Model.Mes_Id contais the selected value, you can do something like this
Html.DropDownListFor 对我不起作用所以我得到了这样的罂粟
Html.DropDownListFor didn't work for me So I got the poppy like this