I saw in another thread that if you want to show a property when viewing the grid but hide it when editing a row one solution was to create a blank editor template for the property and set the dataannotation attribute DisplayName in the viewmodel to "".
This work, almost! The property is showing up in the grid when viewing and hides when editing a row.
But: When trying to save the row I get:
Value cannot be null.Parameter name: value [ArgumentNullException: Value cannot be null.Parameter name: value]System.ComponentModel.DataAnnotations.ValidationContext.set_DisplayName(String value) +51759It seems the the DisplayName cannot be blank after all.
How do I solve this?
5 Answers, 1 is accepted
Unfortunately it is hard to determine where could be the issue. Could you please provide a simple test application which reproduces the described problem. Thus I will be able to review it locally and advice you further.
Georgi Krustev
the Telerik team
I think you could just test to add the attributes to a viewmodel and fake an update.
[DisplayName("")][UIHint("Empty")]public virtual string ResourceName { get; set; }But I think I'll go with a new editor template for the whole viewmodel, I'm feeling kind of locked with this approach. I need more control over the form generated.
Do you have any demo or guides using a complete editor template for the model? Like:
<div class="editor-label"><%= Html.LabelFor(model => model.ResourceName) %></div><div class="editor-field"><%= Html.EditorFor(model => model.ResourceName)%></div>/Mattias
I believe that you should decorate properties, which you need to hide from edit form, with [ReadOnly] attribute. Thus the value of the property will be visible, but will be read-only. Other positive side of this approach is that you should not create additional EditorTemplate for the property of the Model.
For your convenience I have attached a simple test project which shows the suggest approach.
Georgi Krustev
the Telerik team
I added ReadOnly to my property:
[ReadOnly(true)]public virtual string ClientName { get; set; }but when editing a row it still shows the client name along with a textbox.
And if that is the case that the property shows in edit mode but is readonly it really doesn't matter to make it work cause I have another property, ClientID:
[DisplayName("Klient")][UIHint("ClientDropDownList")]public virtual Guid ClientID { get; set; }which shows a dropdownlist of clients and this is the field I want to show when editing.
I don't know, is there something wrong with my design?
I just want to show the client name in the grid when viewing and be able to select among clients when editing!
/Mattias
Could you please verify that you are using latest official release of Telerik Components for ASP.NET MVC (version 2010.3.1110) ? If this is not the case I will ask you to send us a simple test project, which reproduce the depicted behavior in order to review implementation. Otherwise I will not be able to help you any further.
Georgi Krustev
the Telerik team