Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have student name dropdownlist Suppose I want to select name from dropdown if I click on add button after that selected name display as it is please help me I am using mvc with Razor.
<select  id="studentId" name="studentName" class="form-control" onchange="changestudent();">
                            <option value="0">select</option>
                           @{
              List<string> studentnames = student.DataLayer.Models.student.getAllstudentName();

                                  foreach (string studenttname in studentnames)
                                  {
                                      <option value="@studentname" >@studentname</option>
                                  }
                            }
                       </select></string>
Posted
Updated 26-Sep-14 2:53am
v3
Comments
[no name] 26-Sep-14 5:24am    
you can do that in jquery, javascript, how do you want that

you can use strongly typed view and return the model from the btn click post event.

for example:-
C#
[HttpPost]
public ActionResult Save(Student sdtudentModel)
{
//To your work
return View(studentModel)
}


Then in View you can use the code to bind the dropdown List
HTML
@Html.DropDownListFor(model => model.StudentID, Model.StudentsList)


StudentID will be the property that will store the selected Value
StudentIDList will be IEnumerable<SelectListItem> that will store all the dropdown values.
Both of these will the properties in your Model.

By this dropdown will retain value on postback(till the returned model has value for StudentID)
 
Share this answer
 
v2
If you are using for loop to bind the options then in changestudent() call a webmethod to save the selected value in ViewBag and use ViewBag to show the value selected.
 
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