Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable edits for certain columns in popup editor?

ASP.Net MVC 3, Kendo UI Web, Kendo grid control.

I configured several fields to be read-only:

.Model(model =>
      {
          model.Field(it => it.foobar).Editable(false);
          ...

and it works as expected in inline mode. But when I switch the mode to popup, the editor show entire structure, so I can edit any field I want (those marked as non-editable as well).

So how to mark them so the popup editor would show only those marked for edits?

like image 396
greenoldman Avatar asked Oct 28 '12 19:10

greenoldman


3 Answers

Popup editor uses MVC's editor template, which is completely independent of Kendo's. If you want to tag that field as readonly, you need to attach a metadata attribute in the model in code. Ex:

public class MyClassUsedInGrid
{
    [System.ComponentModel.DataAnnotations.Editable(false)]
    public string foobar {get;set;}
}

Update:

Apologize, the answer was incomplete originally. You need to create a custom template to handle this, as built in ones don't support it (I had this in my project and forgot about it). To do this, create a view under /Views/Shared/EditorTemplates/string.cshtml (I'm going to be showing this in Razor, it's easy to port to aspx syntax though).

The code would look like the following:

@model string
@if(ViewData.ModelMetadata.IsReadOnly){
    @Html.DisplayForModel()
}else{
    @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @class = "text-box single-line" })
}

This will properly process Editable metadata attribute. Of course this is just for string, you should do something similar for other objects. If you're looking for what other build in templates look like, check this site: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html

Performance Sidenote: If you only do this in one place in few places in application, it's probably better to place it under /Views/YourView/EditorTemplates/ instead. The reason is the built in templates are compiled into the framework and will work faster in general. Alternatively leave it in Shared folder, but name it something like ExtendedString, and then in you view tag properties which you set Editable with explicit UI hint like so:

[System.ComponentModel.DataAnnotations.Editable(false)]
[System.ComponentModel.DataAnnotations.UIHint("ExtendedString")]
public string foobar {get;set;}
like image 99
Andrew Stakhov Avatar answered Jan 01 '23 22:01

Andrew Stakhov


According to KendoUI documentation (http://docs.telerik.com/kendo-ui/aspnet-mvc/validation) you should anotate the propety with [HiddenInput(DisplayValue = false)]

Example:

[HiddenInput(DisplayValue = false)]
public int Id { get; set; }

Tested and works.

like image 43
Nikolay Kostov Avatar answered Jan 01 '23 23:01

Nikolay Kostov


A similar, simpler but less general solution is to use a custom Editor Template:

column.Bound(a => a.foobar).EditorTemplateName("Empty");

Then put "Empty.cshtml" in your Views/Shared/EditorTemplates with nothing in it.

like image 22
Curtis Yallop Avatar answered Jan 01 '23 21:01

Curtis Yallop