Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a model like below

C#
public class Role
    {
        public int RoleID { get; set; }
        public string RoleName { get; set; }
    }


and i have a @html.DropDown controller in my view. I have a table in sqlserver having two columns RoleID and RoleName. I retrieved the data from this table into a dataset. How can i populate the Dropdown list using this dataset. if i select a role from this dropdownlist, i want to save RoleID into another table dbo.user. How can i do that. Please Help me...


Thanks and Regards
Kunjammu
Posted
Updated 10-Jul-13 21:14pm
v3

try this one,..take asp dropdownlist,..

private void BindBatch()
        {
            ddlBatch.Items.Clear();
            DataSet dsBatch --------------------------------this is ur dataset 
            if (dsBatch != null && dsBatch.Tables.Count > 0 && dsBatch.Tables[0].Rows.Count > 0)
            {
                ddlBatch.DataSource = dsBatch;
                ddlBatch.DataTextField = "RoleName";
                ddlBatch.DataValueField = "RoleID";
                ddlBatch.DataBind();
            }
            ddlBatch.Items.Insert(0, "Select");
        }


and in inserting area u have to save ddlBatch.SelectedValue 


Accept as answer if solve you problem.
 
Share this answer
 
Comments
Kunjammu 11-Jul-13 3:53am    
i am using MVC4....
i used
@Html.DropDownListFor(model => model.User.RoleID, (IEnumerable<SelectListItem>)ViewData["Role"])

in my view

in controller

public IEnumerable<SelectListItem> List
        {
            get
            {
                UserDAL userDAL = new UserDAL();
                DataSet ds = new DataSet();
                int RoleID = Convert.ToInt32(Session["RoleID"].ToString());
                ds = userDAL.UserRoleRetrieve(RoleID);
                DataTable dt = ds.Tables[0];
                List<SelectListItem> list = new List<SelectListItem>();
                foreach (DataRow row in dt.Rows)
                {
                    list.Add(new SelectListItem
                    {
                        Value = Convert.ToString(row["RoleID"]),
                        Text = Convert.ToString(row["RoleName"]),
                    });
                }
                return list;
            }
        }


and

SQL
SelectList selectList = new SelectList(List, "Value", "Text");
            ViewData["Role"] = selectList;
 
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