Click here to Skip to main content
15,886,632 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

1 solution

This has been resolved now.

My dropdown razor code should be
@Html.DropDownListFor(model => model.selectedid, Model.ddllist)

since my ddllist property is already a List<selectlistitem>, it works fine with this code.
 
Share this answer
 

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