Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
i wanna set my selected state into this state dropdownlist. i dont have any idea to set this . please help some one with this below code.

Model(ClaimModel ):
C#
public DataSet Bindcarrier()
        {
            //StudentContext db = new StudentContext();
            try
            {
                con.Open();
                DataSet ds = new DataSet();
                if (con.State == ConnectionState.Open)
                {
                    SqlCommand cmd = new SqlCommand("usp_ddlb_ref_carrier_login", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@pi_is_active", 1);
                    cmd.Parameters.AddWithValue("@pi_login_id", DBNull.Value);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);

                    da.Fill(ds);
                    return ds;
                }
                else
                {
                    return ds;
                }

            }
            finally
            {
                con.Close();
            }
        }


View:

@Html.DropDownList("state", null, new { @class = "drplist" })

Controller:
C#
[HttpGet]
    public ActionResult Create(Boolean Name)
    {                   ClaimModel obj = new ClaimModel();
                        DataSet ds = obj.Bindstate();

                        ViewBag.fname = ds.Tables[0];
                        List<SelectListItem> items = new List<SelectListItem>();
                        foreach (System.Data.DataRow dr in ViewBag.fname.Rows)
                        {
                            items.Add(new SelectListItem { Text = @dr["state_short_name"].ToString(), Value = @dr["state_id"].ToString() });
                        }

                        ViewBag.State = items;

      }
Posted
Updated 28-Aug-18 3:48am
v3

SelectListItem[^] has property Selected...set it for the right item and it will be the selected one...
C#
items.Add(new SelectListItem { Text = @dr["state_short_name"].ToString(), Value = @dr["state_id"].ToString(), Selected = (@dr["state_id"] == any_index) });
 
Share this answer
 
Comments
Deenuji 23-Dec-14 3:13am    
thank you peter. but its selected only in list value but its not reflecting in dropdownlist . we need add any selected item parameter in dropdownlist?
I use DropdownListFor()

Which has a slightly different approach I use a model not a view bag. A model is a separate class called by the Controller. An example is given in this stack overflow c# - MVC ViewModel example - Stack Overflow[^]


For this to work in your model you need 2 properties.
public List<SelectListItem> items {get;set;}
public int MyAssignedToValue {get;set;}

then in your view
@Html.DropdownListFor(a=> a.MyAssignedToValue, Model.items)
 
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