DROP Down List绑定 [英] DROP Down List binding

查看:81
本文介绍了DROP Down List绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我需要一个注册页面,其中有一个性别选项。所以我需要为此创建一个类。



那么我需要什么性别数据类型(枚举,收集,整数或字符串)



For公共示例????性别{get; set;}



????可能是Enum,collection,int或String ....



什么是最好的....

Hi Everybody,

I need a Registration Page in which have an option of Gender. So i need to Create a class for that.

So what should i need to Gender datatype (Enum, Collection , Int or String )

For Example Public ???? Gender {get;set;}

???? may be Enum,collection,int or String....

what is the best for that ....

推荐答案

添加一个包含所有枚举的类(如果有的话)



添加枚举如下:



Add a class which hold all your enumerations (if more)

Add enum as below:

public enum Gender
   {
       male = 1,
       female = 2,
       transgender = 3
   }







在C#代码中,将其绑定如下:






In C# code, bind it as below:

foreach (var gen in typeof(Gender).GetFields())
            {
                if (gen.FieldType == typeof(Gender))
                    DropDownList1.Items.Add(new ListItem { Text = gen.Name, Value = gen.GetRawConstantValue().ToString() });
            }







谢谢,

Prashant
标记为解决方案,如果这适合您!




Thanks,
Prashant
Mark as Solution if this works for you!


在控制器中

In Controller
public ActionResult SelectCountry() {

     List<selectlistitem> items = new List<selectlistitem>();

     items.Add(new SelectListItem { Text = "India", Value = "0",Selected = true});

     items.Add(new SelectListItem { Text = "Nepal", Value = "1" });

   

     ViewBag.Country = items;

     return View();

 }
</selectlistitem></selectlistitem>





In查看



@ Html.DropDownList(国家)





为您的应用程序您可以使用喜欢



public List< selectlistitem>性别

{

get

{

List< selectlistitem> items = new List< selectlistitem>();



items.Add(new SelectListItem {Text =Male,Value =2,Selected = true}) ;



items.Add(new SelectListItem {Text =Female,Value =2});



退货;



}

}



可能适合你!



谢谢



In View

@Html.DropDownList("Country")


For Your Application You can Use Like

public List<selectlistitem> Gender
{
get
{
List<selectlistitem> items = new List<selectlistitem>();

items.Add(new SelectListItem { Text = "Male", Value = "2",Selected = true});

items.Add(new SelectListItem { Text = "Female", Value = "2" });

return items;

}
}

Its May Suit for you !

Thanks


这篇关于DROP Down List绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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