Category / Section
How to return the model value of DropDownList to the controller using scaffolding method?
2 mins read
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.
- 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;
}
}
- 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;
}
- Create a strong typed view based on your model.
C#
@Html.EJ().DropDownListFor(Model => Model.DropData, (Syncfusion.JavaScript.Models.DropDownListProperties)ViewData["properties"])
- 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();
}
- 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.
