Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written code for model is:
C#
public ActionResult Edit(int id = 0)
       {
           StudendDetails objStudent = new StudendDetails();
           if (id == 0)
           {
               return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
           }
           else
           {
               Repository objCommmon = new Repository();

               List<SelectListItem> genderList = objCommmon.GetGenderList();
               List<SelectListItem> StatusList = objCommmon.GetStatus();

               objStudent = objConnection.Studend_Details.Where(x => x.Roll_No == id).SingleOrDefault();


               ViewBag.Gender = genderList.Select(x => x).ToList().Select(x => new SelectListItem
               {
                   Value = x.Value.ToString(),
                   Text = x.Text.ToString(),
                   Selected = (x.Value.ToString() == objStudent.Gender ? true : false)
               });



               ViewBag.Status = StatusList.Select(x => x).ToList().Select(x => new SelectListItem
                       {
                           Value = x.Value.ToString(),
                           Text = x.Text.ToString(),
                           Selected = (x.Value.ToString() == objStudent.Status ? true : false)
                       });


                   return View("StudentMain", objStudent);


               }

           }

In the view


HTML
@Html.DropDownListFor(model => model.Gender,(IEnumerable<SelectListItem>)ViewBag.Gender,  new { id = "ddlGender", style = "width: 150px;" }).


 @Html.DropDownListFor(model => model.Status, (IEnumerable<SelectListItem>)ViewBag.Status, new { id = "ddlStatus", style = "width:150px" }) 



When i retrieve existing record from model then textbox is being filled but in case of DropDownListFor Not select from exiting value .

Please help me any one.
Posted
Updated 24-Apr-15 1:04am
v3

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