Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table in database which stores all the value about the user when the user registers.i want to retrive those values in the form in edit mode. i have successfully got all values in a list, and is getting visible in textbox in edit mode of the form. but there is a dropdown in the form too, i am not able to get how to set the default value of the dropdown based on my value coming from the databse which is presently in list.

DataLayer
C#
public List<registerviewmodel> GetUserdetail(string Role)
        {
            try
            {
                cmd = new SqlCommand();
                RegisterViewModel mvm;
                List<registerviewmodel> lstMVM = new List<registerviewmodel>();
                cmd.CommandText = "getUserdetail";
                if (!String.IsNullOrEmpty(Role))
                {
                    cmd.Parameters.Add("@Role", SqlDbType.NVarChar).Value = Role;
                }
                DataTable dtModel = dl.GetSpData(cmd);
                foreach (DataRow dr in dtModel.Rows)
                {
                    mvm = new RegisterViewModel();
                    mvm.Id = dr["Id"].ToString();
                    mvm.CustomerName = dr["Customer Name"].ToString();
                    mvm.Company = dr["Company"].ToString();
                    mvm.Address = dr["Address"].ToString();
                    mvm.City = dr["City"].ToString();
                    mvm.RegionAbri = dr["Region"].ToString();
                    mvm.PostalCode = dr["Postal Code"].ToString();
                    mvm.CountryId = int.Parse(dr["CountryId"].ToString());
                    mvm.CountryName = dr["CountryName"].ToString();
                    mvm.PhoneNumber = dr["Phone Number"].ToString();
                    mvm.Fax = dr["Fax"].ToString();
                    mvm.Email = dr["Email"].ToString();
                    mvm.AckEmail = dr["AckEmail"].ToString();
                    mvm.ApplicationDate = dr["Application Date"].ToString();
                    mvm.UserVerified = dr["User Verified"].ToString();
                    mvm.UserStatus = dr["User Status"].ToString();
                    mvm.CustomerNo = dr["CustomerNo"].ToString();
                    mvm.Password = dr["PasswordHash"].ToString();
                    mvm.Multiplier = dr["Multiplier"].ToString();
                    mvm.QuoteExpire = dr["QuoteExpire"].ToString();
                    mvm.Freight = dr["Freight"].ToString();
                    mvm.Rep = dr["Rep"].ToString();
                    mvm.ISENumber = dr["ISENumber"].ToString();
                    mvm.ReceiveMail = dr["ReceiveMail"].ToString();
                    mvm.BuyingGroup = dr["BuyingGroup"].ToString();
                    mvm.Comments = dr["Comments"].ToString();
                    lstMVM.Add(mvm);
                }
                return lstMVM;
            }
            catch (Exception e)
            {
                throw e;

            }
        }


Controller
C#
public ActionResult UserMaintainance()

       {
           eQuoteDataAccess eqda = new eQuoteDataAccess();
           RegisterViewModel model = new RegisterViewModel();
           model.UserList = eqda.GetUserdetail(Role);
           return View(model);
       }

View
HTML
@foreach (var item in Model.UserList)
{ @Html.DropDownListFor(modelitem => item.CountryId,Model.Country)}</registerviewmodel></registerviewmodel></registerviewmodel>
Posted
Updated 7-Dec-14 18:51pm
v5

1 solution

After your DataBind():

dropDown.SelectedIndex = 0;
or
dropDown.SelectedValue = "anyValue";
or
dropDown.Items.FindByValue("anyValue").Selected = true;
or
dropDown.Items.FindByText("anyValue").Selected = true;

Try any of this, this might work :)

-KR
 
Share this answer
 
Comments
nikita patidar 4-Dec-14 9:31am    
thanx for reply, but i am getting a list of details, and among those list there is a country value. When i want to edit the complete details of user, i need to display the value he selected in dropdown at the time of registeration. i have country id and country name in the userlist already. I am not getting how to display that in dropdown.

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