【问题标题】:Database First Validation with asp.net使用 asp.net 进行数据库首次验证
【发布时间】:2014-08-01 09:37:01
【问题描述】:

我遵循了以下教程: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>
}

【问题讨论】:

  • 我的元数据文件是否必须与模型类位于同一命名空间中?

标签: c# asp.net validation data-annotations ef-database-first


【解决方案1】:

这可能有几个原因。以下是其中一些。

  1. 您需要客户端验证(javascript 验证)吗?也许您禁用了客户端验证,或者您不需要在视图或布局中链接的 javascript 文件。

  2. 也许您没有在视图中使用@Html 方法来显示表单,尤其是表单字段。例如@Html.TextBoxFor

查看ASP.NET MVC Client Side Validation

【讨论】:

  • 一开始我确实认为这是问题所在,但是服务器端验证也没有发现错误?
  • 确定吗?请查看 ModelState.IsValid。您的 viewModel(因此您在视图中使用的模型)是否有注释? (例如Required(ErrorMessage = "This field is requied"))
猜你喜欢
  • 1970-01-01
  • 2020-03-29
  • 1970-01-01
  • 2018-07-08
  • 2021-04-28
  • 2017-04-13
  • 2018-09-26
  • 1970-01-01
  • 2016-01-11
相关资源
最近更新 更多