I am busy trialing KendoUI Complete for MVC with a purchase coming month end.
I have a KendoUI Grid in place with the editor mode being set to GridEditMode.PopUp.
I have created an EditorTemplate for the popup and this is working great for editing existing records. Everything 100% here.
My problem comes in with creating a new one. The editor only shows 2 fields - Description and Date (Textbox and DatePicker).
When I click the Update button in the editor I get a validation message "The Key Field Is Required" and cannot create the new record.
I have decorated the key property in my model with Editable(false), Browsable(false) and ReadOnly(true) and still the message persists.
The key field is not even displayed in the Editor, so why is there validation on it?
The key field is a Guid type.
Any ideas how to get around this?
Another smaller issue, is I cannot resize the editor. I have tried ".Window(window => window.HtmlAttributes(new { @style = "width:500px;" }))" but it has zero effect. The editor is always the same default size no matter what I do.
TIA
Mike
7 Answers, 1 is accepted
I discovered my key field was still being validated even though my editor view used "@Html.HiddenFor(model => model.ObjectId)".
The only way around I could figure out is to make my ObjectId key field a nullable property which feels more like a hack. It is a key field so being nullable does not make any sense, but at least HiddenFor does not add validation to it now.
MVC implicitly adds required validation for value types. Besides making the field Nullable you could also disable the automatic required validation e.g.
protected
void
Application_Start()
{
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes =
false
;
As for the Window width - not all properties are serialized when using Ajax binding. In order to set the width, you could either set the styles for the edit container:
.k-edit-form-container
{
width
:
500px
;
}
$(
function
() {
var
grid = $(
"#grid"
).data(
"kendoGrid"
);
grid.options.editable.window.width = 500;
});
Daniel
the Telerik team
Adding
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
I changed my ObjectId back to a non nullable field
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ObjectId { get; set; }
I am not getting the KendoUI validation on the ObjectId now, but after clicking Update I am getting a popup dialog saying "The Following errors have occurred: A value is required.".
I tried the above with both the HiddenFor included and excluded from the code as you suggested. Made no difference.
Sorry, I missed that the server validation will be triggered. You can avoid this behavior by specifying a default value for the field through the DataSource Model:
.Model(model =>
{
model.Id(o => o.ObjectId );
model.Field(o => o.ObjectId).DefaultValue(Guid.Empty);
})
Daniel
the Telerik team
My appreciated.
MVC implicitly adds required validation for value types. Besides making the field Nullable you could also disable the automatic required validation e.g.[/quote]
However, Asp.Net MVC (with jQuery & unobtrusive validation) does not implicitly validate hidden fields. They are normally skipped...for any other n00bs' sake.
[quote]Daniel said: If the ID in the editor template is not needed in another scenario, you could also remove the hidden input. The fields are kept in the dataSource model so the values will still be sent. [/quote]
Thank you for mentioning this. And if it is needed for another scenario, it could possibly be placed in some hidden element other than an input.