2

In my ASP.NET MVC Core 1.1.1 I've a form with one input as follows where user is required to enter the district code as two characters such as 01, 02,...10,11, etc. But when I submit the form by entering district code as, say, 123, it still successfully submits the form without forcing user to enter the district code as two characters. What I may be missing?

MyViewModels

...
[Display(Name = "District"),StringLength(2)]
public string Dist { get; set; }
...

Form

@model MyProj.Models.MyViewModel
...
<div class="form-group">
    <label asp-for="Dist" class="col-md-2 control-label"></label>
    <div class="col-md-2">
        <input asp-for="Dist" class="form-control" />
        <span asp-validation-for="Dist" class="text-danger"></span>
    </div>
    <div class="col-sm-pull-8">01 to 53 — district codes</div>
</div>
...

NOTE I am using default ASP.NET Core Web Application template with latest version of VS2017 that by default installs necessary Bootstrap and other javascripts when one uses such template.

nam
  • 21,967
  • 37
  • 158
  • 332
  • Have you added the client javascript for validation? Try to check, in the server (action) is the ModelState is valid and if the invalid check if the reason is the Dist Property, if it says it is invalid what happens is that you are not validating at the client (javascript) – dime2lo Aug 28 '17 at 22:04
  • @dime2lo I've added a **NOTE** in the post above to answer your question that other readers may have as well. – nam Aug 28 '17 at 22:14
  • 1
    have you checked how the model is arriving at the server (if ModelState is valid)? If is invalid, as it should be, then the problem is in the client. Maybe I´m wrong but you have to add the section for javascript client side validation. `@section Scripts { @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } }` – dime2lo Aug 28 '17 at 22:20
  • 1
    how do you register the MVC middleware( `.AddMvc` or `.AddMvcCore`). I am asking cause MVC data annotation services should be registered and this is done automatically only with `AddMVC`, otherwise you need directly call `AddMvcCore().AddDataAnnotations()`. See https://stackoverflow.com/q/44979691/2833802 – Set Aug 29 '17 at 08:31
  • @Set The `ConfigureServices(...)` in `StartUp.cs` has `AddMvc();` in it. – nam Aug 29 '17 at 23:43
  • @dime2lo Is the following for partial view only: `@section Scripts { @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } }`. My view is not a partial view. – nam Sep 01 '17 at 19:12

1 Answers1

4

1 - The first parameter of StringLength is Maximum length. To define minimum you can do:

    [Display(Name = "District")]
    [Required]
    [StringLength(2, MinimumLength = 2)]
    public string Dist { get; set; }

or:

    [Display(Name = "District")]
    [Required]
    [MinLength(2)]
    [MaxLength(3)]
    public string Dist { get; set; }
  1. The Required attribute is missing!

View

Validation script:

@section Scripts { @Html.Partial("_ValidationScriptsPartial") }

Controller

[HttpPost]
    public IActionResult Contact(MyViewModel model)
    {
        if (ModelState.IsValid)
        { ... }
SolidSk
  • 56
  • 1
  • 3