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.

6
  • 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) Commented Aug 28, 2017 at 22:04
  • @dime2lo I've added a NOTE in the post above to answer your question that other readers may have as well. Commented Aug 28, 2017 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"); } } Commented Aug 28, 2017 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 stackoverflow.com/q/44979691/2833802 Commented Aug 29, 2017 at 8:31
  • @Set The ConfigureServices(...) in StartUp.cs has AddMvc(); in it. Commented Aug 29, 2017 at 23:43

1 Answer 1

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)
        { ... }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.