Reputation: 22566
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>
}
Upvotes: 0
Views: 281
Reputation: 60556
This can have several reasons. Here are some of them.
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.
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
Upvotes: 1