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 return the model value of DropDownList to the controller using scaffolding method?

Platform: ASP.NET MVC |
Control: DropDownList |
Published Date: July 29, 2016 |
Last Revised Date: May 8, 2019

Description

Strongly-Typed HTML Helper: The DropDownList control supports strongly typed HTML helpers represented by lambda expressions that have model or template passed into the View. The Extension method is used to get a value from the model. 

Solution

The DropDownListFor takes lambda as a parameter that tells the helper with element of the model to use in the typed view. This enables the better compile-time checking of the views that allow you to find errors at build-time instead of at runtime, and also enables better code intelligence support within the view templates.

The following steps explain how to get the values by using the Scaffolding methods.

  1. Create the model for your requirement.

C#

public class DropDownListModel
    {
        public List<Employee> DropData { get; set; }
    }
    public class Employee
    {
        public string name { set; get; }
        public string empID { set; get; }
        public string desgination { set; get; }
        public Employee(string ename, string eid, string edesg)
        {
            name = ename;
            empID = eid;
            desgination = edesg;
        }
    }

 

  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()
        {
            DropDownListModel model = new DropDownListModel();
            model.DropData = "E11011";
            BindingData();
            return View(model);
         }
        public void BindingData()
        {
            List<Employee> data = new List<Employee>() { };
            data.Add(new Employee("Nancy", "E11011", "Technical Writer"));
            data.Add(new Employee("Angel", "E11012", "Professor"));
            data.Add(new Employee("Daniah", "E11013", "Dancer"));
            data.Add(new Employee("Jenifer", "E11014", "Beautician"));
            data.Add(new Employee("Prince", "E11015", "Developer"));
            DropDownListProperties ddl = new DropDownListProperties();
            ddl.DataSource = data;
            DropDownListFields ddf = new DropDownListFields();
            ddf.Text = "name";
            ddf.Value = "empID";
            ddl.DropDownListFields = ddf;
            ViewData["properties"] = ddl;
        }

 

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

C#

 

 
@Html.EJ().DropDownListFor(Model => Model.DropData, (Syncfusion.JavaScript.Models.DropDownListProperties)ViewData["properties"])

 

  1. Create an action method, FormPost that handles the Post request and processes the data. In the action method, you can pass a model as the parameter. That model has UI input field data.

C#

public ActionResult FormPost(DropDownListModel model)
 {
        return View();
  }

 

  1. Select the values from the EJ form controls and post the form. You can get the selected values in the post action from the model as follows.

 

Return model value using scaffolding method

 

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