0

I have a problem with data validation in my application. Im using a Razor Pages and .Net Core 3.0 framework with EF Core orm. In my model I have two properties:

public float WireCrosssection { get; set; }
public float CableLength { get; set; }

On page, I have inputs for them:

    <div class="form-group">
        <label asp-for="Cable.WireCrosssection"></label>
        <input class="form-control" asp-for="Cable.WireCrosssection" />
        <span class="text-danger" asp-validation-for="Cable.WireCrosssection"></span>
    </div>
    <div class="form-group">
        <label asp-for="Cable.CableLength"></label>
        <input class="form-control" asp-for="Cable.CableLength" />
        <span class="text-danger" asp-validation-for="Cable.CableLength"></span>
    </div>

Client side validation is turned on and this validation doesn't report problems with the form but the server side one do (ModelState.IsValid is false). The number is provided with dot ("."). Any suggestions?

Jacek Majer
  • 345
  • 1
  • 3
  • 14

2 Answers2

1

Be sure you have added the validation script in your view.

@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}

Here is a simple demo like below:

1.Model:

public class Test
{     
    public float WireCrosssection { get; set; }
    public float CableLength { get; set; }
}

2.View:

@model Test
<form asp-action="Index">
    <div class="form-group">
        <label asp-for="WireCrosssection"></label>
        <input class="form-control" asp-for="WireCrosssection" />
        <span class="text-danger" asp-validation-for="WireCrosssection"></span>
    </div>
    <div class="form-group">
        <label asp-for="CableLength"></label>
        <input class="form-control" asp-for="CableLength" />
        <span class="text-danger" asp-validation-for="CableLength"></span>
    </div>
    <div class="form-group">
        <input type="submit" value="Create" class="btn btn-primary" />
    </div>
</form>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

3.Controller:

[HttpPost]
public IActionResult Index(Test test)
{
    //..
}

4.Result: enter image description here

Reference:Client Side Validation

Rena
  • 30,832
  • 6
  • 37
  • 72
  • I have not resolve this issue. Now I know that when client side validation is disabled and I can send the value with comma(1,3) to the server, the model is valid, but when the value is with dot (1.3) the server side validation have errors. Is there any chance to change this on the server side? Change the commas to dots? – Jacek Majer Jan 07 '20 at 18:57
1

Ok, I solved the issue. The problem was a mismatch in culture on the server side and on the client side. Client side validation is written in "en-US" culture. To set the culture on the ASP.NET Core application you need to add following code to the Configure method in the Startup.cs class:

var cultureInfo = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
Jacek Majer
  • 345
  • 1
  • 3
  • 14