Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Get both text and value of selected option in a dropdown in Asp.Net MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 297
    Comment on it

    How to get both text and value of selected option in a dropdown in Asp.Net MVC?

    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.

    The solution that worked for me for the above requirement is as follows:-

    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:-

    In the Model Class:-
    1. public class ParsingLogic
    2. {
    3. public int Id;
    4. public string classificationID;
    5. public string logic;
    6. public List<SelectListItem> classificationIDs;
    7. }
    In the Controller Class:-
    1. public ActionResult Index()
    2. {
    3. ParsingLogic parsingLogic = new ParsingLogic();
    4. parsingLogic.classificationIDs.Add(new SelectListItem { Text = "--Select Classification--", Value = "" });
    5. List<Classification> classifications = smsLogicControllerApi.GetClassificationIds();
    6.  
    7. if (classifications.Count == 1 && classifications[0].status == Status.Error)
    8. {
    9. ModelState.AddModelError("Error", "Problem in getting classification Ids.");
    10. }
    11. else
    12. {
    13. foreach (Classification classification in classifications)
    14. {
    15. parsingLogic.classificationIDs.Add(new SelectListItem { Text = classification.classificationID, Value = Convert.ToString(classification.classificationID + ":" + classification.smsClassificationID) });
    16. }
    17. }
    18. return View(parsingLogic);
    19. }
    In the View:-
    1. @Html.DropDownListFor(model => model.smsClassificationID, Model.classificationIDs, new { id ="classificationID", @class = "form-control" })

    Note:- In the following line of code, I have combined both Value as well as Text:-

    1. 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:-

    1. public ActionResult Update(FormCollection collection)
    2. {
    3. string[] array = Convert.ToString(collection["smsClassificationID"]).Split(':');
    4. string selectedValue = array[0];
    5. string selectedText = array[1];
    6. }

    Hope it helps... Happy Coding!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: