Html.DropDownListFor unable to select from model in mvc 2.0

Jak
Jak
Member
352 Points
151 Posts

Hi,

I am using Html.DropDownListFor as in view page:

<td>
<%:Html.DropDownListFor(m => m.ddlCategory_NewQuestion, Model.Category)%> 
</td>

In Controller:

Model.Category.Add(new SelectListItem { Text = "--Select--", Value = "", Selected = false });
 
var CategoryList = from c in db.tbl_Categories orderby c.Priority select c;
 
foreach (var item in CategoryList)
{
 
if (item.CategoryShortName == "")
{
SelectListItem =new SelectListItem();
SelectListItem.Selected =true;
SelectListItem.Text = item.Category;
SelectListItem.Value = item.CategoryShortName;
Model.Category.Add(SelectListItem);
}
 
else
{
Model.Category.Add(new SelectListItem() { Selected = false, Text = item.Category, Value = item.CategoryShortName });
}
}

In Model:

public class QuestionModel
{
 
public QuestionModel() {
Category =new List<SelectListItem>();
}
[DisplayName("Category")] 
public int ddlCategory_NewQuestion { get; set; } 
public List<SelectListItem> Category { get; set; }
}

 

 

Views: 8130
Total Answered: 1
Total Marked As Answer: 0
Posted On: 09-Jun-2015 02:14

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
Moderator
1170 Points
210 Posts
     

Hi Jak,

take string type of ddlCategory_NewQuestion in the model as:

public class QuestionModel
public QuestionModel() {
Category =new List<SelectListItem>();
}
[DisplayName("Category")] 
public string ddlCategory_NewQuestion { get; set; } 
public List<SelectListItem> Category { get; set; }
}
Posted On: 05-Dec-2015 06:12
banner

Blog

Active User (0)

No Active User!
 Log In to Chat