This is a migrated thread and some comments may be shown as answers.

The Key Field Is Requied problem on creating new record.

7 Answers 177 Views
Editor
This is a migrated thread and some comments may be shown as answers.
DominionZA
Top achievements
Rank 1
DominionZA asked on 26 Mar 2013, 08:11 AM
Hi all,

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

Sort by
0
DominionZA
Top achievements
Rank 1
answered on 26 Mar 2013, 11:48 AM
Okay, I am clearly an MVC4 n00b still.

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.
0
Daniel
Telerik team
answered on 28 Mar 2013, 07:50 AM
Hello Mike,

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;
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. 

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;
}
or set the needed width after the Grid initialization:
$(function () {
    var grid = $("#grid").data("kendoGrid");
    grid.options.editable.window.width = 500;
});
Regards,
Daniel
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
DominionZA
Top achievements
Rank 1
answered on 28 Mar 2013, 07:58 AM
Thanks Daniel. Will give this a bash and revert.
0
DominionZA
Top achievements
Rank 1
answered on 28 Mar 2013, 08:36 AM
Hi Daniel,

Adding
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
did not work unfortunately.

I changed my ObjectId back to a non nullable field
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ObjectId { get; set; }
and added the code above to Application_Start() and tested.
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.
0
Daniel
Telerik team
answered on 29 Mar 2013, 03:48 PM
Hello again Mike,

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);
    })
This should also resolve the problem without disabling the validation. Regards,
Daniel
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Accepted
DominionZA
Top achievements
Rank 1
answered on 02 Apr 2013, 10:08 AM
Thank-you Daniel. This works perfectly now.
My appreciated.
0
Jesse
Top achievements
Rank 1
Veteran
answered on 31 Oct 2017, 10:18 PM
[quote]Daniel said:

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.

Tags
Editor
Asked by
DominionZA
Top achievements
Rank 1
Answers by
DominionZA
Top achievements
Rank 1
Daniel
Telerik team
Jesse
Top achievements
Rank 1
Veteran
Share this question
or