Click here to Skip to main content
15,911,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ....
Posted

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

Add enum as below:

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



In C# code, bind it as below:

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



Thanks,
Prashant
Mark as Solution if this works for you!
 
Share this answer
 
Comments
[no name] 12-Apr-13 7:01am    
Any specific Reason for the Enum. Because that can be String,int or Collection datatype.
PrashantSonewane 12-Apr-13 7:06am    
Thats the implementation choice. However if collection is known and static ( we are sure that gender collection will have these static entries), should go with enum. No need to add database calls and other impplementation. Reduce db call as much as possible. Make sense?
[no name] 12-Apr-13 7:20am    
No I mean only for creating Object no db relation....
Enum,int ,string,collections,Class object these are some choices for that synario .... then which one i need to implement... Enum may be change ex("Select Category","Other")

that is just an example....

so what is the best way to using those problems.

i know that enum not change frequently but may be change. so.....
[no name] 12-Apr-13 7:40am    
There is one more case if i want to select default female then how can i do it by using enum.
PrashantSonewane 12-Apr-13 7:44am    
Choice would be depend on what type of data you are binding. Without looking at that, hard to make decision. Generally we should use collection if data is volatile and stored in database.

Making default is dropdownlists property and not of enum. you can use dropdownlist.items[0].selected=true property by selecting any item of choice.
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 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
 
Share this answer
 
v2
Comments
[no name] 12-Apr-13 8:10am    
Hi , This is fine but the complete question is what is the best data type (Enum,String,int,Class object or etc) for those fields which are not change frequently. For Example : Gender, Married Status ,etc.....

and please provide a reason for that.
rmksiva 12-Apr-13 9:09am    
Thanks for Command
Yes ! Public List<selectlistitem> Gender { get;set;}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900