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
    • 282
    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:-
    public class ParsingLogic
        {
            public int Id;
            public string classificationID;
            public string logic;
            public List<SelectListItem> classificationIDs;
       }
    
    In the Controller Class:-
    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);
            }
    
    In the View:-
    @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:-

    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];
        }
    

    Hope it helps... Happy Coding!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: