I am trying to use a dropdownlist as an editor template (using MVC Complete) and having several issues/questions.
1.) The binding works on Save, but not on Read: i.e. when I select an option and save the model, the correctly selected item's value is saved to the database, but then when pulling up that record again, the DDL is not reflecting that saved value. I have verified that value is in the Database and is in the view model.
2.) Can you bind to the View's model instead of the ViewData? i.e. .BindTo(Model.DDLOptions.CustomerOptions). When I try this I get an error "
3.) Should the .Name value match the Property Name or the name as it is in the model. i.e. since my model has multiple sub-classes, it seems that I need to use a .Name that corresponds to the way the model references that field. In the view, I'd reference the OwnerId field as @Html.EditorFor(model => model.Item.OwnerId). As such I am using a .Name("Item.OwnerId"). This results in the issues listed in item 1...only 1 way binding. If I change it to .Name("OwnerId"), the base classes property name, no binding occurs at all.
Property decorated with UIHint in MetaData class for Base Class: This is for the Item class
View Model
EditorTemplate
How this property is reference in the View
If I place the Kendo DDL directly in the view, without using the editortemplate, here is the HTML output for Input element:
When the Kendo DDL is displayed via the EditorTemplate, here is the resulting HTML
Note in the latter it is missing the Value=3 attribute as well as the validation attributes.
Any help is appreciated.
Chad
1.) The binding works on Save, but not on Read: i.e. when I select an option and save the model, the correctly selected item's value is saved to the database, but then when pulling up that record again, the DDL is not reflecting that saved value. I have verified that value is in the Database and is in the view model.
2.) Can you bind to the View's model instead of the ViewData? i.e. .BindTo(Model.DDLOptions.CustomerOptions). When I try this I get an error "
'int' does not contain a definition for 'DDLOptions'
3.) Should the .Name value match the Property Name or the name as it is in the model. i.e. since my model has multiple sub-classes, it seems that I need to use a .Name that corresponds to the way the model references that field. In the view, I'd reference the OwnerId field as @Html.EditorFor(model => model.Item.OwnerId). As such I am using a .Name("Item.OwnerId"). This results in the issues listed in item 1...only 1 way binding. If I change it to .Name("OwnerId"), the base classes property name, no binding occurs at all.
Property decorated with UIHint in MetaData class for Base Class: This is for the Item class
[Display(Name = "Owner Id")]
[UIHint("CustomersAllDDLEditor")]
public Nullable<
int
> OwnerId { get; set; }
public class ItemViewModel
{
public Item Item { get; set; }
public ItemOptionsViewModel DDLOptions { get; set; }
public AuditStampsViewModel AuditStamps { get; set; }
}
@(Html.Kendo().DropDownList()
.Name("Item.OwnerId")
.DataValueField("Id")
.DataTextField("DisplayName")
//.BindTo(Model.DDLOptions.CustomerOptions)
.BindTo((System.Collections.IEnumerable)ViewData["customers"])
.OptionLabel(" ")
.Template("#= data.DisplayName.replace(' ', ' ') #")
)
@Html.EditorFor(model => model.Item.OwnerId)
<
input
data-val
=
"true"
data-val-number
=
"The field Owner Id must be a number."
id
=
"Item_OwnerId"
name
=
"Item.OwnerId"
type
=
"text"
value
=
"3"
class
=
"k-input valid"
data-role
=
"dropdownlist"
style
=
"display: none;"
>
<
input
id
=
"Item_OwnerId"
name
=
"Item.OwnerId"
type
=
"text"
class
=
"k-input valid"
data-role
=
"dropdownlist"
style
=
"display: none;"
>
Any help is appreciated.
Chad