This is a migrated thread and some comments may be shown as answers.

Editor Template - Can't get to bind on Read

1 Answer 147 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Chad
Top achievements
Rank 1
Chad asked on 21 Dec 2012, 12:17 AM
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 "

'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; }
View Model
public class ItemViewModel
    {
        public Item Item { get; set; }
        public ItemOptionsViewModel DDLOptions { get; set; }
        public AuditStampsViewModel AuditStamps { get; set; }
    }
EditorTemplate
@(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(' ', ' ') #")
)
How this property is reference in the View
@Html.EditorFor(model => model.Item.OwnerId)
If I place the Kendo DDL directly in the view, without using the editortemplate, here is the HTML output for Input element:
<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;">
When the Kendo DDL is displayed via the EditorTemplate, here is the resulting HTML
<input id="Item_OwnerId" name="Item.OwnerId" type="text" class="k-input valid" data-role="dropdownlist" style="display: none;">
Note in the latter it is missing the Value=3 attribute as well as the validation attributes.

Any help is appreciated.
Chad

1 Answer, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 24 Dec 2012, 12:09 PM
Hi Chad,

 Up to your questions:

1) Can you confirm that your Save action method correctly updates the underlying database with the right value? If it does the right value should be returned during Read.

2) You probably can however in your case the Model property is actually an "int" (hence the error message). You can change the model of your partial view using the @model directive

3) We recommend using DropDownListFor(model => model) in such cases. Then setting Name is not needed as we are using ASP.NET MVC to generate it for us.

If you have further questions please send us a runnable subset of your project which we can test locally.

Regards,
Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
General Discussions
Asked by
Chad
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Share this question
or