使用mvc在下拉列表中显示值 [英] displaying values in dropdownlist using mvc

查看:99
本文介绍了使用mvc在下拉列表中显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,比如我希望将所有国家/地区名称显示在sql数据库表的下拉列表中除了加拿大,我能够显示所有国家/地区名称但不能显示所有国家/地区名称除了加拿大可以任何人帮助我out,我使用下面的代码在下拉列表中显示所有国家/地区名称





@ Html.DropDownListFor(model => model .nationality,

(SelectList)@ ViewBag.NationalityDropDownValues,---选择国籍---,新{@class =m-wrap})









提前致谢

解决方案

< blockquote>你对加拿大有什么看法?



填充ViewBag.NationalityDropDownValues时只包括非加拿大国家。你没有在你填充该属性的地方发布代码,所以我们不能给你特定的帮助,但它可能就像



 ViewBag.NationalityDropDownValues = countries.Where(c => C.Name!=Canada); 









 ViewBag.NationalityDropDownValues = countries.Where(c => C.ID!= 123); 





其中123是您所在国家/地区的加拿大ID。如果您在需要显示加拿大的地方使用ViewBag.NationalityDropDownValues,那么最好将您的视图传递给两个集合,一个集合中包含加拿大,另一个集合已过滤。


I have a requirement like i want to display all the country names into dropdown from sql database table except canada , i am able to display all the country names but not able to display all the country names except canada can any one help me out, i am using below code for displaying all the country names in dropdown list


@Html.DropDownListFor(model => model.nationality,
(SelectList)@ViewBag.NationalityDropDownValues, "---Select Nationality ---", new { @class = "m-wrap " })




Thanks in advance

解决方案

What do you have against Canada?

When you populate ViewBag.NationalityDropDownValues only include the non-Canadian countries. You haven't posted the code where you populate that property so we can't give you specific help but it'll probably be something like

ViewBag.NationalityDropDownValues = countries.Where(c => C.Name != "Canada");



or

ViewBag.NationalityDropDownValues = countries.Where(c => C.ID != 123);



where "123" is the ID for Canada in your country collection. If you use ViewBag.NationalityDropDownValues in places that do need to show Canada then it's best to pass your view two collections, one with Canada in it and one filtered.


这篇关于使用mvc在下拉列表中显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆