某些表单字段正在获取其他人没有的验证消息?

时间:2016-06-10 14:06:45

标签: asp.net asp.net-mvc asp.net-mvc-4

我正在使用ASP.Net MVC 4,Bootstrap 3,EF6和AdventureWorks数据库创建一个项目。使用数据库第一种方法生成EF模型。在我正在创建的产品表单中,所有字段都是必需的,但是,某些字段会出现验证错误,而其他字段则不然。所有文本字段都没有获得验证错误,而所有数字字段都正确显示验证错误。这是一张图片: enter image description here

以下是我的观点:

<h2>Add A Product</h2>
<div class="container">
@using (Html.BeginForm())
{


    <div class="row">
        <div class="col-md-3">@Html.LabelFor(model => model.Name, new { @class = "form-control" })</div>
        <div class="col-md-6">
            @Html.TextBoxFor(model => model.Name, new { @class = "form-control" })
            @Html.ValidationMessageFor(model=>model.Name)
        </div>
    </div>

    <div class="row">
        <div class="col-md-3">@Html.LabelFor(model => model.ProductNumber, new { @class = "form-control" })</div>
        <div class="col-md-6">
            @Html.TextBoxFor(model => model.ProductNumber, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.ProductNumber)
        </div>
    </div>

    <div class="row">            
        <div class="col-md-3">@Html.LabelFor(model => model.MakeFlag, new { @class = "form-control" })</div>
        <div class="col-md-3">@Html.CheckBoxFor(model => model.MakeFlag, new { @class = "checkbox" })</div>
    </div>

    <div class="row">
        <div class="col-md-3">@Html.LabelFor(model => model.FinishedGoodsFlag, new { @class = "form-control" })</div>
        <div class="col-md-3">@Html.CheckBoxFor(model => model.FinishedGoodsFlag, new { @class = "checkbox" })</div>           
    </div>

    <div class="row">
        <div class="col-md-3">@Html.LabelFor(model => model.Color, new { @class = "form-control" })</div>
        <div class="col-md-6">
            @Html.TextBoxFor(model => model.Color, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Color)
        </div>
    </div>



    <div class="row">
        <div class="col-md-3">@Html.LabelFor(model => model.SafetyStockLevel, new { @class = "form-control" })</div>
        <div class="col-md-6">
            @Html.TextBoxFor(model => model.SafetyStockLevel, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.SafetyStockLevel)
        </div>
    </div>

    <div class="row">
        <div class="col-md-3">@Html.LabelFor(model => model.ReorderPoint, new { @class = "form-control" })</div>
        <div class="col-md-6">
            @Html.TextBoxFor(model => model.ReorderPoint, new { @class = "form-control" })
            @Html.ValidationMessageFor(model=>model.ReorderPoint)
        </div>
    </div>


    <div class="row">
        <div class="col-md-3">@Html.LabelFor(model => model.StandardCost, new { @class = "form-control" })</div>
        <div class="col-md-6">
            @Html.TextBoxFor(model => model.StandardCost, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.StandardCost)
        </div>
    </div>

    <div class="row">

        <div class="col-md-3">@Html.LabelFor(model => model.ListPrice, new { @class = "form-control" })</div>
        <div class="col-md-6">
            @Html.TextBoxFor(model => model.ListPrice, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.ListPrice)
        </div>
    </div>

    <div class="row">
        <div class="col-md-3">@Html.LabelFor(model => model.DaysToManufacture, new { @class = "form-control" })</div>
        <div class="col-md-6">
            @Html.TextBoxFor(model => model.DaysToManufacture, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.DaysToManufacture)
        </div>
    </div>

    <div class="row">
        <div class="col-md-3">@Html.LabelFor(model => model.SellStartDate, new { @class = "form-control" })</div>
        <div class="col-md-6">
            @Html.TextBoxFor(model => model.SellStartDate, new { @class = "form-control datepicker", placeholder = "Enter sell start date here..." })
            @Html.ValidationMessageFor(model => model.SellStartDate)
        </div>            
    </div>
    <div class="row">
        <div class="col-md-6">
            <button type="submit" class="btn btn-info col-md-2">Submit</button>
        </div>            
    </div>
}
</div>




@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
@*@Scripts.Render("~/bundles/jquery-ui")*@
<script src="~/Scripts/jquery-ui-1.11.4.min.js"></script>



<script type="text/javascript">
    $('.datepicker').datepicker(); //Initialise any date pickers
</script>

<script>
    $(document).ready(function () { $(".datepicker").datepicker({}); });

</script>
}

我的模型是默认的EF模型,没有任何字段的[Required]注释:

public int ProductID { get; set; }
public string Name { get; set; }
public string ProductNumber { get; set; }
public bool MakeFlag { get; set; }
public bool FinishedGoodsFlag { get; set; }
public string Color { get; set; }
public short SafetyStockLevel { get; set; }
public short ReorderPoint { get; set; }
public decimal StandardCost { get; set; }
public decimal ListPrice { get; set; }
public string Size { get; set; }
public string SizeUnitMeasureCode { get; set; }
public string WeightUnitMeasureCode { get; set; }
public Nullable<decimal> Weight { get; set; }
public int DaysToManufacture { get; set; }
public string ProductLine { get; set; }
public string Class { get; set; }
public string Style { get; set; }
public Nullable<int> ProductSubcategoryID { get; set; }
public Nullable<int> ProductModelID { get; set; }
public System.DateTime SellStartDate { get; set; }
public Nullable<System.DateTime> SellEndDate { get; set; }
public Nullable<System.DateTime> DiscontinuedDate { get; set; }
public System.Guid rowguid { get; set; }
public System.DateTime ModifiedDate { get; set; }

所有文本字段的Nullable属性设置为false:

enter image description here

为什么数字字段在字符串不被验证时会被验证?

4 个答案:

答案 0 :(得分:2)

扩展@ErikPhilips答案。

.NET具有值类型和引用类型。数值通常是值类型,例如int,double等。字符串是引用类型。

值类型必须具有值(即,它不能为null),并且MVC框架通过创建所需的值类型来强制执行此操作,无论其是否存在必需属性。换句话说,您不能将null赋给int或double,因此MVC会抛出错误。

这就是为什么某些值被验证(值类型)而有些值不是(引用类型)的原因。您将注意到某些值类型包含在Nullable包装器中,这些类型也将不会被验证。

答案 1 :(得分:1)

它们在数据库中定义&#34; NO-NULL&#34;。

建议:

using System.ComponentModel.DataAnnotations;

[Required]
[Display(Name = "Days To Manufacture")]
public int DaysToManufacture { get; set; }

答案 2 :(得分:1)

首先,您不应将EF模型用作视图模型。而是使用表单所需的属性创建新的视图模型。之后用[Required]标签装饰属性。

此外,不要忘记检查接收表单数据的控制器操作中的模型状态,因为JavaScript验证不能保护您不接收无效的表单数据。您可以使用以下代码执行此操作:

if(ModelState.IsValid)
{
    //do some stuff with form data
}
else 
    return View(viewModel);

如果您想了解有关MVC验证的更多信息,可以阅读this帖子

答案 3 :(得分:1)

  

某些表单字段正在获取其他人没有的验证消息吗?

不可为空的属性类型(int,decimal,short,bool等)需要一个值(null / empty字符串不能解析为默认值)。默认情况下,string可以为空,因此它不必具有值。它真的很简单。