Click here to Skip to main content
16,017,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
@Html.DropDownListFor(model => model.name, new SelectList(new List<Object>{
new { value = "gg" , text = name1 },
new { value = "hh" , text = name2 },
new { value = "jj" , text = name3},
new { value = "kk" , text = name4 }
}, "value", "text"), new { id = "ddlClass", @class = "field04" })
Posted
Comments
♥…ЯҠ…♥ 26-Nov-13 6:30am    
are you getting error here?
aswinkavumpurath 26-Nov-13 6:34am    
no but the problem was am passing the value to model like model.name ="kk" but its not fetching the text from drop down, always show name1. is there any solution for this?

1 solution

Hi buddy,

You just need to do below thing,

Create a property in your Model class

public List<selectlistitem> Names{get;set;}.

Then just create method in model class

Public List<selectlistitem> GetNames()
{
var names=new List<selectlistitem>();

C#
names.add(new SelectListItem{
              Text="1",
              Value="1"});


SQL
names.add(new SelectListItem{
              Text="1",
              Value="1"});



SQL
names.add(new SelectListItem{
              Text="1",
              Value="1"});


return names;
}


Then in constructor of model

public ModelClassName()
{
Names=GetNames();
}

Then, call this model from controller and pass the model to view


Now you just have to right in view

C#
@Html.DropDownListFor(model => model.name, model.Names, new { id = "ddlClass", @class = "field04" })




If you find this useful then please vote for this.
Thanks.
 
Share this answer
 
v2

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