The Key Field Is Requied problem on creating new record.

7 posts, 1 answers
  1. DominionZA
    DominionZA avatar
    49 posts
    Member since:
    Jul 2007

    Posted 26 Mar 2013 Link to this post

    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
  2. DominionZA
    DominionZA avatar
    49 posts
    Member since:
    Jul 2007

    Posted 26 Mar 2013 Link to this post

    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.
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 28 Mar 2013 Link to this post

    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!
  5. DominionZA
    DominionZA avatar
    49 posts
    Member since:
    Jul 2007

    Posted 28 Mar 2013 Link to this post

    Thanks Daniel. Will give this a bash and revert.
  6. DominionZA
    DominionZA avatar
    49 posts
    Member since:
    Jul 2007

    Posted 28 Mar 2013 Link to this post

    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.
  7. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 29 Mar 2013 Link to this post

    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!
  8. Answer
    DominionZA
    DominionZA avatar
    49 posts
    Member since:
    Jul 2007

    Posted 02 Apr 2013 Link to this post

    Thank-you Daniel. This works perfectly now.
    My appreciated.
Back to Top
UI for ASP.NET MVC is VS 2017 Ready