Showing posts with label mvc. Show all posts
Showing posts with label mvc. Show all posts

Jan 31, 2018

ASP.NET MVC custom culture client validation

Further info here: http://www.c-sharpcorner.com/article/internationalization-in-asp-net-core-mvc/

Watch for @inject ... for Localization property

Install NuGet packages:
- jquery-globalize.0.1.3
- jquery.validation
- Microsoft.jQuery.Unobtrusive.Validation

Choose your culture and properly initialize globalize library:

    <script src="~/Scripts/globalize.0.1.3/globalize.js"></script>

    <script src="~/Scripts/globalize.0.1.3/cultures/globalize.culture.hr.js"></script>
    <script>

        $(document).ready(function () {

            Globalize.culture("hr");

            $.validator.methods.number = function (value, element) {

                return this.optional(element) || !isNaN(Globalize.parseFloat(value));

            }

            $.validator.methods.range = function (value, element, param) {

                return this.optional(element) || (Globalize.parseFloat(value) >= param[0] && Globalize.parseFloat(value) <= param[1]);

            }

            $.validator.methods.date = function (value, element) {

                return this.optional(element) || Globalize.parseDate(value);

            };

        });

    </script>

This handles check on client side on input values.

I haven't tried but for input validation there is DataFormatString param validation:


[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? BirthDate { get; set; }

https://stackoverflow.com/questions/13916991/mvc-datatype-errormessage

Oct 16, 2015

ASP.NET MVC routing ordering

Always place more specific route before less specific in your routes definitions ....
And I always forget it.
More specific route includes your parameters and so :
        
    routes.MapRoute("ReportsWithoutMISCode",
              "Reports/{Action}/{programObjectiveId}/{userName}",
              new { controller = "Reports", action = "Index" },
              new { programObjectiveId = @"\d+" },
              new[] { "Web.Controllers.Reports" });
     routes.MapRoute("ReportsWithMISCode",
              "Reports/{Action}/{programObjectiveId}/{userName}/{MISCode}",
              new { controller = "Reports", action = "Index" },
              new { programObjectiveId = @"\d+" },
              new[] { "Web.Controllers.Reports" });

; should be switched since second route ReportsWithMISCode is more specific, means having more parameters.

So it should be:

     routes.MapRoute("ReportsWithMISCode",
              "Reports/{Action}/{programObjectiveId}/{userName}/{MISCode}",
              new { controller = "Reports", action = "Index" },
              new { programObjectiveId = @"\d+" },
              new[] { "Web.Controllers.Reports" });
      routes.MapRoute("ReportsWithoutMISCode",
              "Reports/{Action}/{programObjectiveId}/{userName}",
              new { controller = "Reports", action = "Index" },
              new { programObjectiveId = @"\d+" },
              new[] { "Web.Controllers.Reports" });

It is interesting to note that in first case route works in awkward way. It gets matched but third parameter MISCode is appended as query param with question mark like this:

...ReportXXX/101/pt2?MISCode=MyMISCode

After switching to second solution we get friendly url.