0

I followed the following tutorial: http://www.elevenwinds.com/data-validation-in-asp-net-mvc-database-first which explains how to add a partial class with metadata in it where I can add validation. I have added every thing which he has added but for some reason my ModelState.isValid() still passes.

Here is the code for the metadata class:

namespace Model.Metadata.RoutingDbV
{
   [MetadataType(typeof(Client.Metadata))]
   public partial class Client
   {
      private sealed class Metadata
      {
         [Required(ErrorMessage = "This field is requied")]
         public int CustIdentifier { get; set; }
         [Required(ErrorMessage = "This field is requied")]
         public string ClientID { get; set; }
         [Required(ErrorMessage = "This field is requied")]
         public string CompanyName { get; set; }
         public string Details { get; set; }
         public bool RoutingEnabled { get; set; }
         public bool TestAccount { get; set; }           
      }
   }
}

Here is a copy of the code generated by the database-first model:

namespace Model
{
  using System;
  using System.Collections.Generic;

  public partial class Client
  {
    public Client()
    {
        this.BaseClients = new HashSet<BaseClient>();
        this.IpRoutings = new HashSet<IpRouting>();
        this.RadioRoutings = new HashSet<RadioRouting>();
        this.SerialRoutings = new HashSet<SerialRouting>();
    }

    public int CustIdentifier { get; set; }
    public string ClientID { get; set; }
    public string CompanyName { get; set; }
    public string Details { get; set; }
    public bool RoutingEnabled { get; set; }
    public bool TestAccount { get; set; }   
  }
}

Now when I submit a form that is completely empty, it doesn't throw any errors? I'm sure there is a small error in the way it is linking or matching up the two partial classes?

//EDIT:

Here is my controller:

if (ModelState.IsValid)
{
    client.ClientID = client.ClientID.ToUpper();
    db.Clients.Add(client);
    await db.SaveChangesAsync();
    return RedirectToAction("Index");
}

Here is my view:

@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Client</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.ClientID, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.ClientID, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.ClientID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.CompanyName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.CompanyName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.CompanyName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Details, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Details, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Details, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.RoutingEnabled, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <div class="checkbox">
                @Html.EditorFor(model => model.RoutingEnabled)
                @Html.ValidationMessageFor(model => model.RoutingEnabled, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.TestAccount, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <div class="checkbox">
                @Html.EditorFor(model => model.TestAccount)
                @Html.ValidationMessageFor(model => model.TestAccount, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>

    @*<div class="form-group">
        @Html.LabelFor(model => model.FSKCustomerId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.FSKCustomerId, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.FSKCustomerId, "", new { @class = "text-danger" })
        </div>
    </div>*@

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}
1
  • Does my metadata file have to be in the same namespace as the model class? Commented Aug 1, 2014 at 9:40

1 Answer 1

1

This can have several reasons. Here are some of them.

  1. Do you want client validation (javascript validation)? Maybe you have disabled client validation or you dont have needed javascript files linked in your view or layout.

  2. Maybe you dont used the @Html methods in your view to show the form, especially the form fields. e.g. @Html.TextBoxFor

Check out ASP.NET MVC Client Side Validation

Sign up to request clarification or add additional context in comments.

2 Comments

I did start out thinking this was the issue, however the server side validation doesn't catch the error either?
Sure?, check out ModelState.IsValid. Did your viewModel (so the model you are using in your view) have the annotations ? (e.g. Required(ErrorMessage = "This field is requied"))

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.