I have the following code in my model:
public class ProjectViewModel
{
[Display(Name = "Project Status")]
public int SelectedStatusId { get; set; }
public SelectList StatusList { get; set; }
}
Following is the class (which is inside the Repository Folder) that retrieves set of status from database:
public IEnumerable<SelectListItem> LoadStatus()
{
using (ProjectEntities proj = new ProjectEntities())
{
var statList = proj.ProjectStatus.Select(x => new SelectListItem
{
Value = x.ProjStatusID.ToString(),
Text = x.ProjStatusCode
});
return new SelectList(statList, "Value", "Text");
}
}
Following is my code on Controller:
public class ProjectMasterController : Controller
{
public ActionResult LoadProjectStatus()
{
ProjectDataManager pm = new ProjectDataManager();
var model = new ProjectViewModel();
model.StatusList = new SelectList(pm.LoadStatus(), "ProjStatusID", "ProjStatusCode");
return View(model);
}
}
And, finally the View:
@Html.LabelFor(model => model.SelectedStatusId)
@Html.DropDownListFor(model => model.SelectedStatusId, Model.StatusList,"Select Status")
I have referred the answer1 and answer2 on StackOverflow. But I am getting an error,
The ViewData item that has the key 'SelectedStatusId' is of type 'System.Int32' but must be of type 'IEnumerable'.
I am not sure where i am going wrong