-
Get both text and value of selected option in a dropdown in Asp.Net MVC
about 9 years ago
about 9 years ago
While working in a MVC project, I got a requirement where I needed both Value as well as
Text of the selected option in the Dropdown.
I combined both Text and Value using ':' between the two and stored it inside SelectListItem.Value
and when the Form was posted back, I used Split(':') function to get both of them.
Here is the code:-
- public ActionResult Index()
- {
- ParsingLogic parsingLogic = new ParsingLogic();
- parsingLogic.classificationIDs.Add(new SelectListItem { Text = "--Select Classification--", Value = "" });
- List<Classification> classifications = smsLogicControllerApi.GetClassificationIds();
- if (classifications.Count == 1 && classifications[0].status == Status.Error)
- {
- ModelState.AddModelError("Error", "Problem in getting classification Ids.");
- }
- else
- {
- foreach (Classification classification in classifications)
- {
- parsingLogic.classificationIDs.Add(new SelectListItem { Text = classification.classificationID, Value = Convert.ToString(classification.classificationID + ":" + classification.smsClassificationID) });
- }
- }
- return View(parsingLogic);
- }
public ActionResult Index() { ParsingLogic parsingLogic = new ParsingLogic(); parsingLogic.classificationIDs.Add(new SelectListItem { Text = "--Select Classification--", Value = "" }); List<Classification> classifications = smsLogicControllerApi.GetClassificationIds(); if (classifications.Count == 1 && classifications[0].status == Status.Error) { ModelState.AddModelError("Error", "Problem in getting classification Ids."); } else { foreach (Classification classification in classifications) { parsingLogic.classificationIDs.Add(new SelectListItem { Text = classification.classificationID, Value = Convert.ToString(classification.classificationID + ":" + classification.smsClassificationID) }); } } return View(parsingLogic); }
Note:- In the following line of code, I have combined both Value as well as Text:-
- parsingLogic.classificationIDs.Add(new SelectListItem { Text = classification.classificationID, Value = Convert.ToString(classification.classificationID + ":" + classification.smsClassificationID) });
parsingLogic.classificationIDs.Add(new SelectListItem { Text = classification.classificationID, Value = Convert.ToString(classification.classificationID + ":" + classification.smsClassificationID) });
Now On Form submit I can retrieve the Text as well as the Value of the selected Option by splitting them as follows:-
- public ActionResult Update(FormCollection collection)
- {
- string[] array = Convert.ToString(collection["smsClassificationID"]).Split(':');
- string selectedValue = array[0];
- string selectedText = array[1];
- }
public ActionResult Update(FormCollection collection) { string[] array = Convert.ToString(collection["smsClassificationID"]).Split(':'); string selectedValue = array[0]; string selectedText = array[1]; }
Can you help out the community by solving one of the following Javascript problems?
Do activity (Answer, Blog) > Earn Rep Points > Improve Rank > Get more opportunities to work and get paid!
For more topics, questions and answers, please visit the Tech Q&A page.
0 Comment(s)