We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to get the selected values in postback in DropdownlistFor control ?

Platform: ASP.NET MVC |
Control: DropDownList |
Published Date: June 22, 2017 |
Last Revised Date: May 7, 2019

Description

The DropDownList control supports strongly typed HTML helpers represented by lambda expressions that have model or template passed into the View.

 

Solution

You can get the selected values of  DropDownListFor control in postback by passing the parameter as a string array in code behind.

 

The following steps to explain how to get the selected item in post back.

  1. Create the model as shown below code:

 

C#

public class TestDroDownModel  
{ 
    public string DropDownItems { get; set; } 
}  
 

 

  1. Create an action method that renders UI on the view page, and passes the model to be bound to the view page.

C#

 public ActionResult DropdownlistFeatures()
        {
            DropDownListProperties ddlval = new DropDownListProperties();
            ddlval.DataSource = GetDataSource();
            DropDownListFields ddf = new DropDownListFields();
            ddf.Text = "Text";
            ddf.Id = "uniqueKey";
            ddf.Value = "Value";
            ddf.Selected = "Selected";
            ddlval.DropDownListFields = ddf;
 
            ViewData["ddl"] = ddlval;
            return View();
         }
 

 

 

 

  1. Create a strong typed view based on your model.

CSHTML

@using (Html.BeginForm())
 {
          @Html.ValidationSummary(true) 
                    
          @Html.EJ().DropDownListFor(model => model.ddl, (Syncfusion.JavaScript.Models.DropDownListProperties)ViewData["ddl"]).DropDownListFields(df=>df.Text("Text").Value("Value").Selected("Selected")).ShowCheckbox(true).WatermarkText("Select a car").Width("300px")
 
                         @Html.EJ().Button("btn").Size(ButtonSize.Small).Text("Test").Type(ButtonType.Submit).Width("150")
 }
 
  1. Get the selected item in code behind as shown below:

 

C#

       [HttpPost] 
        public ActionResult DropdownlistFeatures(string[] DropDownItems) 
        { 
             
            return View(); 
        }  
 

 

   

You can get the selected values in the post action as follows:

Selected values in dropdown postback

2X faster development

The ultimate ASP.NET MVC UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon