我遵循了以下教程:http://www.elevenwinds.com/data-validation-in-asp-net-mvc-database-first,它解释了如何在其中添加包含元数据的部分类,我可以在其中添加验证。我添加了他添加的所有内容但由于某种原因我的ModelState.isValid()
仍然通过。
以下是元数据类的代码:
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; }
}
}
}
以下是数据库优先模型生成的代码的副本:
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; }
}
}
现在当我提交一个完全空的表单时,它不会抛出任何错误?我确定链接或匹配两个部分类的方式有一个小错误?
//编辑:
这是我的控制器:
if (ModelState.IsValid)
{
client.ClientID = client.ClientID.ToUpper();
db.Clients.Add(client);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
以下是我的观点:
@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>
}
答案 0 :(得分:1)
这可能有几个原因。以下是其中一些。
您想要客户端验证(javascript验证)吗?也许您已禁用客户端验证,或者您在视图或布局中没有链接所需的javascript文件。
也许您没有在视图中使用@Html
方法来显示表单,尤其是表单字段。例如@Html.TextBoxFor