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?
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;}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With