Working with nullable fields

10 posts, 1 answers
  1. Jon
    Jon avatar
    6 posts
    Member since:
    Jun 2012

    Posted 30 Oct 2012 Link to this post

    I am trying to use a NumericTextBox with a nullable decimal. When the numeric text box is left blank, the default "The field <field name> must be a number" message appears.

    How can I submit the data with an empty NumericTextBox to pass a null value to my controller?

    Thanks!
  2. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 02 Nov 2012 Link to this post

    Hello Jon,

    I am not able to reproduce such behavior. Could you please check the attached project and see if I missed something?
    If I did please modify the project and send it back so we can search for a solution.

    Kind regards,
    Petur Subev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Jon
    Jon avatar
    6 posts
    Member since:
    Jun 2012

    Posted 07 Nov 2012 Link to this post

    I guess it is more than just the numeric text box. It also has to do with using it in a grid popup editor.

    I have updated your project with the issue. If you try to add a person and leave the salary blank, you will see the error.

    Thanks.

    EDIT: Well, I can't re-attach the updated project because it exceeds the 2MB limit. All I did was add a grid and an EditorTemplate for the Person object.

    EditorTemplates\Person.cshtml:
    @model KendoMVCWrappers.Models.Person
     
    @Html.LabelFor(m => m.Name):<br />
    @Html.EditorFor(m => m.Name)
    @Html.ValidationMessageFor(m => m.Name)
    <br /><br />
    @Html.LabelFor(m => m.BirthDate):<br />
    @Html.Kendo().DatePickerFor(m => m.BirthDate)
    @Html.ValidationMessageFor(m => m.BirthDate)
    <br /><br />
    @Html.LabelFor(m => m.Salary):<br />
    @Html.Kendo().NumericTextBoxFor(m => m.Salary)
    @Html.ValidationMessageFor(m => m.Salary)
    <br /><br />

    Grid in Index.cshtml
    @(Html.Kendo().Grid<KendoMVCWrappers.Models.Person>()
        .Name("Persons")
        .HtmlAttributes(new { style = "height:200px;" })
        .Scrollable()
        .Columns(columns =>
        {
            columns.Bound(p => p.Name);
            columns.Bound(p => p.Salary);
        })
        .ToolBar(tb => tb.Create().Text("Add Person"))
        .Editable(editable => editable
            .Mode(GridEditMode.PopUp)
            .Window(win => win.Title("Add Person"))
        )
        .DataSource(ds => ds
            .Ajax()
            .Model(model =>
            {
                model.Id(p => p.PersonID);
                model.Field(p => p.PersonID).Editable(false);
            })
            .Create(create => create.Action("CreatePerson""Home"))
            .Read(read => read.Action("ReadPersons""Home"))
        )
    )

  5. Jesse
    Jesse avatar
    3 posts
    Member since:
    Mar 2012

    Posted 07 Nov 2012 Link to this post

    I don't know if this is the kind of solution you are looking for, but I was able to get around this problem by specifying a default value on those fields:

    @(Html.Kendo().Grid<MyModel>()
        .Name("grid")
        @* other settings here *@
        .DataSource(dataSource => dataSource
            .Ajax()
            .Model(model =>
            {
                model.Id(m => m.MyModelId);
                model.Field(f => f.Salary).DefaultValue(0);        
            })
        )
    )

  6. Jon
    Jon avatar
    6 posts
    Member since:
    Jun 2012

    Posted 07 Nov 2012 Link to this post

    @Jesse,

    Thanks, but that's not really what I'm looking for. I need to the value to be null, not zero. By setting a default value, the value is in the numeric text box when creating the record.

    When not being used as part of the editor in the grid, it allows submission with an empty (null) value. When added to the editor, it will return an error...
  7. Tomasz
    Tomasz avatar
    9 posts
    Member since:
    Oct 2012

    Posted 17 Jan 2013 Link to this post

    I have same problem. If a property in model is set to etc. decimal? , i can`t save data on the grid.
    I need to leave it as null , and I can`t .
    If I set property in model as string type then it is fine, but it cant be done in my case.

    How to solve this problem ?
  8. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 22 Jan 2013 Link to this post

    Hello Tomasz,

    Could you update the project and send it within a support ticket referencing this forums thread so we can take a look?

    Thank you for the understanding.

    Kind regards,
    Petur Subev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  9. Jon
    Jon avatar
    6 posts
    Member since:
    Jun 2012

    Posted 22 Jan 2013 Link to this post

    I have created ticket # 651165 with an updated project that shows the issue.

    Thanks.

  10. Answer
    Tomasz
    Tomasz avatar
    9 posts
    Member since:
    Oct 2012

    Posted 22 Jan 2013 Link to this post

    I upgrade to new release and it seems to work fine in my project
  11. Jon
    Jon avatar
    6 posts
    Member since:
    Jun 2012

    Posted 23 Jan 2013 Link to this post

    Thanks Tomasz, the upgrade corrected the issue. That is the same response I received from support.
Back to Top
UI for ASP.NET MVC is VS 2017 Ready