Html.DropDownListFor unable to select from model in mvc 2.0

Jak
Jak
908 Points
132 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: 9418
Total Answered: 1
Total Marked As Answer: 0
Posted On: 09-Jun-2015 02:14

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
4916 Points
27 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
 Log In to Chat