Articles in this section
Category / Section

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

1 min read

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

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied