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

Bind edit template to enum type, save int value

2 Answers 235 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 08 Mar 2018, 03:23 PM

First off, I am very new to MVC, so apologies in advance.

I don't know what I'm missing, but I have an enum type:

 public enum ClientStatus
    {
        [Description("Unknown")]
        Unknown = 0,
        [Description("Normal")]
        Normal = 1,
        [Description("Legal Hold")]
        LegalHold = 200
    }

There is a database field which holds the integer value corresponding to the enum type.  For my grid, I have:

Html.Kendo().Grid<IST.DocstorClient.Database.cache_dsl_client>()
                    .Name("grid")
                    .Columns(columns =>
                    {
                        columns.Bound(o => o.dsl_client_id).Title("Client ID").EditorTemplateName("#dsl_client_id#");
                        columns.Bound(o => o.dsl_client_name).Title("Client Name");
                        columns.Bound(o => o.dsl_client_description).Title("Description");
                        columns.Bound(o => o.dsl_client_status).Title("Status").ClientTemplate("#=status_name#").EditorTemplateName("ClientStatus");
                        columns.Command(cmd => cmd.Destroy());
                    })
                    .ToolBar(toolbar =>
                    {
                        toolbar.Create();
                        toolbar.Save();
                    })
                    .Editable(editable => editable.Mode(GridEditMode.InCell))
                    .Pageable()
                    .Navigatable()
                    .PersistSelection()
                    .Sortable()
                    .DataSource(dataSource => dataSource
                        .Ajax()
                        .Batch(true)
                        .PageSize(10)
                        .Model(model =>
                        {
                            model.Id(p => p.dsl_client_id);
                            model.Field(p => p.dsl_client_id).Editable(false);
                        })
                        .Events(events => events.Error("dsr_error_handler"))
                        .Read("Editing_Read", "Client")
                        .Create("Editing_Create", "Client")
                        .Update("Editing_Update", "Client")
                        .Destroy("Editing_Destroy", "Client")

 

The item in question here is the column for dsl_client_status, which is defined as a byte? type in the Entity Framework definitions.

I also have a dropdown definition in Shared/EditorTemplates:

@(Html.Kendo().DropDownListFor(m => m)
   .Name("ClientStatus")
   .DataTextField("Text")
   .DataValueField("Value")
   .BindTo(EnumHelper.GetSelectList(typeof(IST.DocstorClient.Data.ClientStatus)))
   .OptionLabel("Select Status")
   .Value(((int)Model).ToString())
  )

 

I have been through a lot of iterations of different things, this is just the state things are in at the moment.

When I click on the cell to edit, I do get the dropdown list, but it is defaulted to the "Select Status", as if there is no value assigned.  I can then change the dropdown value, but the database value never gets updated.  If I click on it again, the value I changed to shows on the dropdown.

For example:

initial value is "Unknown".  
I click to edit, the dropdown list defaults to "Select Status".  
I change to "Normal" and click Save Changes. Text still shows as "Unknown".  
I click to edit again and the dropdown is defaulted to "Normal"

I've been scouring forums and blogs for days, trying all manner of suggestions, all to no avail.  I find it hard to believe that this is that difficult a task to achieve, so I gotta think I'm missing something simple here.

2 Answers, 1 is accepted

Sort by
0
Steve
Top achievements
Rank 1
answered on 08 Mar 2018, 06:51 PM
Quick followup: I changed the name of the editortemplate file to "dsl_client_status.cshtml".  That seems to have done the trick.
0
Dimitar
Telerik team
answered on 12 Mar 2018, 12:43 PM
Hello Steve,

The recommended approach to achieve the desired behavior is to utilize the UIHint annotation in the model in order to specify the editor template:
public class CustomerViewModel
{
  public int CustomerID{ get; set; }
 
  [UIHint("StatusEditor")] 
  public CustomerStatus Status { get; set; }
}

Then, the DropDownList in the editor template (StatusEditor.cshtml under Views/Shared/EditorTemplates) should be bound as follows:
@(Html.Kendo().DropDownList()
  .Name("Status") // The name of the widget should be the same as the name of the property.
  .DataValueField("StatusID")
  .DataTextField("StatusName")
  .BindTo((System.Collections.IEnumerable)ViewData["statuses"])
)

For more detailed information on templating with Telerik UI for ASP.NET MVC, please refer to the following Editor Templates Documentation.

Regards,
Dimitar
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
DropDownList
Asked by
Steve
Top achievements
Rank 1
Answers by
Steve
Top achievements
Rank 1
Dimitar
Telerik team
Share this question
or