Hi All, I am just trying to bind values to a dropdownlist control. I could see no of examples but it is not working for me. Please find my code and the error and help me to solve this.
//My model
public class SubjectModel
{
public int selectedid {get;set;}
public List<SelectListItem> ddllist{get;set;}
public SubjectModel()
{
ddllist = new List<SelectListItem>();
}
}
//My controller
public ActionResult DropDownTest()
{
SubjectModel model = new SubjectModel();
model.ddllist.Add(new SelectListItem { Text = "Physics", Value = "1" });
model.ddllist.Add(new SelectListItem { Text = "Chemistry", Value = "2" });
model.ddllist.Add(new SelectListItem { Text = "Mathematics", Value = "3" });
model.selectedid = 0;
return View(model);
}
//My View
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dopdownbinding.models.subjectmodel>"%>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" c/>
<title>DropDownBinding</title>
</head>
<body>
<label for="timezone">Subjects</label>
@Html.DropDownListFor(model => model.selectedid, new SelectList(Model.ddllist, "SubCategoryId", "SubCategoryName", Model.selectedid))
</body>
</html>
I am getting this error while executing this.
An expression tree may not contain a dynamic operation
I am new to ASP.Net MVC and please help me correct if something is wrong.Thanks