I am a new in asp.net mvc 2.0, I tried to search about this article but still can not get the answer like what I want.
I have one form to assign the role to each employee. So I create one form that I can input the employee's name and select the role that they are in. The role are taking from table Role. I used linq to sql to query the RoleName and RoleID from table Role, and want to bind it to DropDownListFor in my view.
I have one model :
public class UserModels
{
public string name { get; set; }
public string role { get; set; }
}
This is what I did in my controller :
[HttpPost]
public ActionResult UserMaintenance(FormCollection frm)
{
if (ModelState.IsValid)
{
EMP_DBSEntities context = new EMP_DBSEntities();
tblUserLogin user = new tblUserLogin();
user.UserName = frm["userLogin"].ToString();
IEnumerable<SelectListItem> role_list = context.tblRoles.Select(d => new SelectListItem
{
Value = d.RoleID.ToString(),
Text = d.RoleName
});
context.AddTotblUserLogins(user);
context.SaveChanges();
return View();
}
else
{
return View();
}
}
Can anyone tell me how could I bind the role_list to my DropDownListFor<> in my view.
Thanks.