MVC 5 not validating StringLength attribute properly

asp.net-mvc asp.net-mvc-5 c# entity-framework model

Question

In my PersonPaymentDetails model, I'm attempting to validate the sortCode field, however my view is failing to validate the StringLength (6). Incorrectly, the form successfully validates if I submit it with a length of 1.

Am I making a basic error here?

/* [CONTROLLER] */
public class PersonController : Controller
{
  [HttpGet]
  [Route("person/paymentDetails/create/{personId?}")]
  public ActionResult PaymentDetailsCreate(int? personId)
  {
      if (personId == null)
      {
          return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
      }
      Person person = db.People.Find(personId);

      if (person == null)
      {
          return HttpNotFound();
      }

      PersonPaymentDetailsViewModel personPaymentDetailsVM = new PersonPaymentDetailsViewModel();
      personPaymentDetailsVM.SetPerson(person);

      return View(personPaymentDetailsVM);
  }

  [HttpPost]
  [Route("person/paymentDetails/create")]
  public ActionResult PaymentDetailsCreate(PersonPaymentDetailsViewModel personPaymentDetailsVM)
  {
      if (ModelState.IsValid)
      {
          /* should not be entering here with sortCode = 123, as not 6 characters in length */
          return Content("No errors: |" + personPaymentDetailsVM.SinglePaymentDetails.sortCode + "|");
      }
  }
}

/* [VIEW] */
@model X.ViewModels.PersonPaymentDetailsViewModel

@Html.ValidationSummary()
@using (Html.BeginForm("PaymentDetailsCreate", "Person", FormMethod.Post, new { @class = " form-horizontal" }))
{
    @Html.HiddenFor(m => m.Person.id, "default")

    <div class="form-group">
        <label for="bankSortCode" class="col-md-3 control-label">Sort Code</label>
        <div class="col-md-9">
            @Html.EditorFor(m => m.SinglePaymentDetails.sortCode, new { htmlAttributes = new { @class = "form-control" } })
        </div>
    </div>

    <div class="form-group">
        <label for="save" class="col-md-3 control-label">&nbsp;</label>
        <div class="col-md-9">
            <button type="submit" class="btn btn-primary">Save</button>
        </div>
    </div>
}

/* [MODEL] */
public partial class PersonPaymentDetails
{
    public int id { get; set; }

    [Required, StringLength(6)]
    public string sortCode { get; set; }
}

/* [ViewModel] */
public class PersonPaymentDetailsViewModel
{
    public Person Person { get; set; }
    public PersonPaymentDetails SinglePaymentDetails { get; set; }

    public void SetPerson(Person person)
    {
        this.Person = person;
        this.SinglePaymentDetails = new PersonPaymentDetails();
    }
}
1
1
11/19/2014 1:56:12 PM

Accepted Answer

You need

[Required, StringLength(6, MinimumLength = 6)]

Architect ofStringLength only accepts the maximum length, therefore in the form that you have it in, it verifies that the string is not longer than 6 characters, in which case the string of length 1 passes validation.

8
11/19/2014 12:21:42 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow