aspnetmvc-11784-components-data-editors-extensions-common-concepts-binding-data-editors-to-data.md
You can bind DevExpress MVC data editors to data in the following ways:
@Html.DevExpress().TextBox(settings => {
settings.Name = "ProductName";
// ...
}).Bind(Model.ProductName).GetHtml()
Important
To bind DevExpress MVC editors, you must use the DevExpressEditorsBinder model binder instead of the default model binder. For more information, see the following section: Specify a Model Binder for Editors
List editors implement the BindList method that allows you to bind an editor to an item list.
@Html.DevExpress().ComboBox(settings => {
settings.Name = "CategoryID";
settings.Properties.TextField = "CategoryName";
settings.Properties.ValueField = "CategoryID";
settings.Properties.ValueType = typeof(int);
}).BindList(((GridViewController)ViewContext.Controller).GetCategories()).Bind(Model.CategoryID).GetHtml()
Data editors use values from bound model properties. Setting initial values at the control settings level does not work.
Set an initial value for such editors at the model level.
DevExpress MVC editors may contain multiple input elements. The default model binder does not correctly match posted editor values with model properties. Use the DevExpressEditorsBinder model binder instead to correctly transfer values from editors back to corresponding data model fields. Otherwise, model properties can have incorrect values when they submit a form with MVC editors.
Tip
DevExpressEditorsBinder is automatically defined as a default model binder when you create a project based on DevExpress MVC templates.
Assign DevExpressEditorsBinder to the ModelBinders.Binders.DefaultBinder property to replace the global default model binder:
protected void Application_Start(){
// ...
ModelBinders.Binders.DefaultBinder = new DevExpress.Web.Mvc.DevExpressEditorsBinder();
}
Declare DevExpressEditorsBinder as a parameter attribute in an action method:
[HttpPost]
public ActionResult ModelBinding([ModelBinder(typeof(DevExpressEditorsBinder))] MyModelData myModel) {
// ...
}
You can use MVC editors in unbound mode. Assign a custom value to the Name property.
@Html.DevExpress().TextBox(settings =>{
settings.Name = "PropertyName";
}).Bind(Model.PropertyName).GetHtml()
You can use this name to access the editor’s client-side programmatic object in the JavaScript code.
var text = PropertyName.GetText();
Use the following methods to retrieve the editor values on the server:
GetSelectedValues for multi-select editors.DevExpress.Web.Mvc.EditorExtension.GetValue<string>("PropertyName");
View Example: Data Editors for ASP.NET MVC - How to use data editors to edit Model fields