使用 MVC 在 Html.DropDownListFor() 中选择一个项目
我有一个种类表(集合),在编辑视图(在 MVC 中)中需要在下拉菜单中使用这种种类,我也需要下拉菜单选择旧种类作为默认值,但存在一些问题。
这是我的选择列表:
var Kinds = db.Kinds
.Where(n => n.Member.Id == owner.Id)
.Select(k =>k.Title);
var selectList = new SelectList(Kinds);
另外,默认种类(值)是:
var Selection= db.Kinds
.Single(k => k.Numbers
.Any(no => no.Id == Model.Id)).Title;
这是我的下拉菜单:
@Html.DropDownListFor(model => model.NumberKind.Title, selectList)
当我定义下拉菜单时,就像在第一个下拉项目中添加的新成员一样:
@Html.DropDownListFor(model => model.NumberKind.Title, selectList, Selection)
例如,如果项目是[Mobile,家庭,工作]
选择是[Home]
使用上面的代码,项目将如下所示:[Home,Mobile,Home,Work]
但我不需要新项目,我只想从主要项目中选择主页 我怎样才能做到这一点?
谢谢
I have a table(Collection) of Kinds And in Edit View (in MVC) Need this Kinds in DropDown, Also I Need The DropDown Select Old Kind as Default, But there is some Problems.
This is my Select List:
var Kinds = db.Kinds
.Where(n => n.Member.Id == owner.Id)
.Select(k =>k.Title);
var selectList = new SelectList(Kinds);
Also The Default Kind(Value) is :
var Selection= db.Kinds
.Single(k => k.Numbers
.Any(no => no.Id == Model.Id)).Title;
And this is My DropDown:
@Html.DropDownListFor(model => model.NumberKind.Title, selectList)
When I Define DropDown Like following the new member added at the first of dropdown items:
@Html.DropDownListFor(model => model.NumberKind.Title, selectList, Selection)
For ex if the items be [Mobile, Home, Work]
And Selection is [Home]
With above Code the items will be like this: [Home, Mobile, Home, Work]
but I don't need new item, I just want Select Home From The Main Items
How Can I do this?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您当前正在为下拉列表提供“默认”值。您正在使用的构造函数的下拉列表的预期目的是将“选择电话”之类的项目添加到下拉列表中。
使用简单:
这里类似的问题供参考: DropDownList 设置在 asp. .net MVC
You're currently providing a "default" value for your drop down list. The intended purpose of the dropdownlistfor constructor you are using would be to add an item like "Select Phone" to your drop down list.
Use simply:
Similar question here for reference: DropDownList setting selected item in asp.net MVC