0
votes

I have a Kendo grid which has custom edit buttons and a toolbar which allows you to add new.

The edit/new functionality uses only one template.

How can I tell if I am in 'Edit' mode vs 'New' mode.

My code(Kendo Grid):

 @(Html.Kendo().Grid(Model.OtherBreeds)
              .Name("grdOtherBreeds")
              .HtmlAttributes(new { @class = "grid" })
              .Columns(columns =>
              {
                  columns.Bound(c => c.OrganizationName);
                  columns.Bound(c => c.Prefix);
                  columns.Bound(c => c.Name);
                  columns.Bound(c => c.MemberId);
                  columns.Template(e => { })
                      .ClientTemplate(KendoTemplates.ActionColumn.WithEdit().WithDelete().Get())
                      .Visible(true)
                      .Title(Resources.Actions)
                      .Width(Constants.GridCommandColumnWidth);
              })
              .DataSource(dataSource => dataSource
                  .Ajax()
                  .PageSize(50) //? standard page size when grid is only UI element on screen?
                  .Model(model =>
                  {
                      model.Id(p => p.Id);
                      model.Field(p => p.OrganizationName);
                      model.Field(p => p.Prefix);
                      model.Field(p => p.Name);
                      model.Field(p => p.MemberId);
                  })
              )
              .Events(e => e.DataBound(KendoEventHandlers.DataBound.WithNoData(Resources.NoOtherBreedsForCustomer).Get()))
              .Sortable()
              .Pageable()
              .Filterable()
              .Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("OtherBreedTemplate").Window(w => w.Width(500).Title("Add Other Breed")))
              .ToolBar(toolbar =>
              {
                  toolbar.Template(KendoTemplates.Toolbar.WithAdd("Add Other Breed").Get());
              })
              )  
1
Is your Id=0 / null when in Add mode?? - ASG
Yes, I've tried checking with JS, but it does not seem to work. I have a function to get the value of Id in my template, but it always logs null. - Mark
there's an Edit event you can capture to get the form data before the popup is shown there should be an e.model.isNew() you can check - JamieD77
Is Id null for both add and edit? It should be a real number for edit and null or 0 or "" depending on datatype for add. - Thomas Koelle

1 Answers

1
votes

you can add an event handler for Edit

.Events(e => e.Edit("onEdit"))

this should give you everything you need for the model and the window

<script>   
    function onEdit(e) {
        if (e.model.isNew()) {
            //the e.container is the PopUp window
            e.container.find(".class").text("New");
        }
    }
</script>