I saw a lot of questions like this but seems I cant find anything to relate one to another and find solution for mine. I am using MVC 4 application where I have a dropdownlist with some values inside.
I am using viewbag to populate it.
ViewBag.Roles = new SelectList(db.Roles, "RoleId", "RoleName");
In the view my dropdown looks like this.
@Html.DropDownList("selectedRole", ViewBag.Roles as SelectList, String.Empty)
The string selectedRole is getting the RoleId value from the view.
After this I am doing some LINQ to the database with selectedRole which says that I cannot convert string to Int inside my LINQ which I really want so I can return better results.
public ActionResult SearchResults(string selectedRole, string selectedCourse, string SearchParam)
{
var members = (from m in db.Members
select m);
if (!String.IsNullOrEmpty(selectedRole))
{
members = (from m in db.Members
where m.UserRole == Int32.Parse(selectedRole)
select m);
}
//some more if's here...
return View(members.ToList());
}
So the question is how to convert this selectedRole to Integer. Are there any rules to follow when converting in LINQ?
PS: I tried also Convert.ToInt32(selectedRole) which returns same error.