Editor Template - Can't get to bind on Read

2 posts, 0 answers
  1. Chad
    Chad avatar
    29 posts
    Member since:
    Nov 2012

    Posted 20 Dec 2012 Link to this post

    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")]
    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; }
        .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.

  2. Atanas Korchev
    Atanas Korchev avatar
    8462 posts

    Posted 24 Dec 2012 Link to this post

    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.

    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!
Back to Top