locked
Data annotation inside MVC6 web grid RRS feed

  • Question

  • User-540818677 posted

    I have added the following model class inside my asp.net MVC core web application 3.1:-

    [ModelMetadataType(typeof(Submission_Validation))]
        public partial class Submission
        {
    
    
        }

    and the following Submission_Validation class:-

    public class Submission_Validation
        {
    
    [DisplayFormat(ApplyFormatInEditMode = true,
    DataFormatString = "{0:dd MMMM yyyy hh:mm:ss tt}")]
    public DateTime Created { get; set; }
        }

    now if i show the Created field using @Html.DisplayFor(model => model.Created) the date will have the correct format as specified inside the data annotation, but if i render the value inside the MVC-6-Grid ( https://mvc6-grid.azurewebsites.net/) as follow:-

    @(Html
        .Grid(Model)
        .Build(columns =>
        {
    columns.Add(model => model.Created).Titled("Created")
    })
        .Using(GridFilterMode.Header)
        .Empty("No data found")
        .Filterable()
        .Sortable()
        .Pageable(pager =>
        {
    
            pager.RowsPerPage = 250;
        })

    the date will ignore the data annotation specified. any idea how i can fix this?

    Monday, June 1, 2020 8:56 PM

Answers

  • User711641945 posted

    Hi johnjohn123123,

    It seems could not apply the data annotation to MVC6 web grid.For your requirement,here is a workaround:

    @(Html.Grid(Model)
        .Build(columns =>
        {
            columns.Add(model => model.Created).Titled("Created").Formatted("{0:dd MMMM yyyy hh:mm:ss tt}");
    })
        .Using(GridFilterMode.Header)
        .Empty("No data found")
        .Filterable()
        .Sortable()
        .Pageable(pager =>
        {
    
            pager.RowsPerPage = 250;
        })
    )

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 2, 2020 9:20 AM

All replies

  • User475983607 posted

    johnjohn123123

    now if i show the Created field using @Html.DisplayFor(model => model.Created) the date will have the correct format as specified inside the data annotation, but if i render the value inside the MVC-6-Grid ( https://mvc6-grid.azurewebsites.net/) as follow:-

    This is the expected result.  Tag and HTML helpers render the HTML and apply the formats as well as the unobtrusive attributes for client side validation.

    johnjohn123123

    the date will ignore the data annotation specified. any idea how i can fix this?

    It is up to you to format the values if you do not use the tag or HTML helpers.  I can't see your HTML but I assume you would use the  .ToString() method.

    Monday, June 1, 2020 9:07 PM
  • User-540818677 posted

    johnjohn123123

    now if i show the Created field using @Html.DisplayFor(model => model.Created) the date will have the correct format as specified inside the data annotation, but if i render the value inside the MVC-6-Grid ( https://mvc6-grid.azurewebsites.net/) as follow:-

    This is the expected result.  Tag and HTML helpers render the HTML and apply the formats as well as the unobtrusive attributes for client side validation.

    johnjohn123123

    the date will ignore the data annotation specified. any idea how i can fix this?

    It is up to you to format the values if you do not use the tag or HTML helpers.  I can't see your HTML but I assume you would use the  .ToString() method.

    my question if i can use the HTML helper inside the web grid?

    Monday, June 1, 2020 10:36 PM
  • User475983607 posted

    my question if i can use the HTML helper inside the web grid?

    I don't think the 3rd party Grid takes advantage of the model annotations.  The formatting documentation clearly shows it is up to the programming to set the formatting using the Formatted() method and standard C# formatting.

    https://mvc6-grid.azurewebsites.net/formatting

    columns.Add(model => model.Birthday).Titled("Birthday").Formatted("{0:d}");

    Monday, June 1, 2020 10:58 PM
  • User711641945 posted

    Hi johnjohn123123,

    It seems could not apply the data annotation to MVC6 web grid.For your requirement,here is a workaround:

    @(Html.Grid(Model)
        .Build(columns =>
        {
            columns.Add(model => model.Created).Titled("Created").Formatted("{0:dd MMMM yyyy hh:mm:ss tt}");
    })
        .Using(GridFilterMode.Header)
        .Empty("No data found")
        .Filterable()
        .Sortable()
        .Pageable(pager =>
        {
    
            pager.RowsPerPage = 250;
        })
    )

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 2, 2020 9:20 AM
  • User-540818677 posted

    Hi johnjohn123123,

    It seems could not apply the data annotation to MVC6 web grid.For your requirement,here is a workaround:

    @(Html.Grid(Model)
        .Build(columns =>
        {
            columns.Add(model => model.Created).Titled("Created").Formatted("{0:dd MMMM yyyy hh:mm:ss tt}");
    })
        .Using(GridFilterMode.Header)
        .Empty("No data found")
        .Filterable()
        .Sortable()
        .Pageable(pager =>
        {
    
            pager.RowsPerPage = 250;
        })
    )

    Best Regards,

    Rena

    ok understood, your code worked for me.

    Tuesday, June 2, 2020 10:41 AM