未编辑字段时触发ValidationMessageFor

时间:2017-02-03 00:39:25

标签: asp.net-mvc

我在编辑屏幕上显示字段。有问题的字段来自模型实体列表。如果我修改了所有屏幕字段的值并按提交,则没有问题。但是,如果存在任何未修改的字段,则会显示RegularExpression消息,即使该条目有效。

ValueDesc字段就是问题。

另外,有没有办法允许在编辑屏幕上输入零而不是添加屏幕?

同样的模型在我的添加屏幕上正常工作。

以下是一些代码:

public class ModLenBinRefModelEntity : IEntityModel
{
    public int AtcId { get; set; }
    public List<LengthBinRefModelEntity> LengthBinReferences { get; set; }

public class LengthBinRefModelEntity : IEntityModel
{
    public short BinId { get; set; }

    [Required(ErrorMessage = "Bin Number is required")]
    [Display(Name = "Bin Number")]
    [RegularExpression(@"^[1-9]\d*", ErrorMessage = "Bin Number must be 1 or greater")]
    public short BinNumber { get; set; }

    [Required(ErrorMessage = "Bin Value Description is required")]
    [Display(Name = "Value Description")]
    [DisplayFormat(DataFormatString = "{0:N1}", ApplyFormatInEditMode = true)]
    [RegularExpression(@"^[1-9]\d*", ErrorMessage = "Value Desc must be 1 or greater")]
    public decimal ValueDesc { get; set; }

...

@model xxxx.xxxxx.UI.Models.ModLenBinRefModelEntity

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.Title = "EditBin";
}
<link href="~/Content/jquery.datetimepicker.css" type="text/css" rel="stylesheet" />
<h2>Edit Length Bin</h2>
<h4>Station @Model.AtcId</h4>

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

        <div class="form-horizontal">
        <hr />

        <table class="table table-striped">
        <tr>
            <th>
                @Html.DisplayName("Bin Number")
            </th>
            <th>
                @Html.DisplayName("Value Description")
            </th>
        </tr>
        @Html.HiddenFor(model => model.AtcId)    

        @for (int i=0; i<Model.LengthBinReferences.Count; i++)
        {
        <tr>
            <td>
                @Html.HiddenFor(model => model.LengthBinReferences[i].AtcId)
                @Html.HiddenFor(model => model.LengthBinReferences[i].BinId)
                @Html.DisplayFor(model => model.LengthBinReferences[i].BinNumber)
                @Html.HiddenFor(model => model.LengthBinReferences[i].BinNumber)
            </td>
            <td>
                @Html.TextBoxFor(model => model.LengthBinReferences[i].ValueDesc)
                @Html.ValidationMessageFor(model => model.LengthBinReferences[i].ValueDesc)
            </td>

        </tr>
        }
        </table>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Modify" class="btn btn-primary" />
                @Html.ActionLink("Cancel", "Details", new { id = Model.AtcId }, new { @class = "btn btn-default" })
            </div>
        </div>
    </div>   
}

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script src="~/Scripts/jquery.datetimepicker.full.min.js"></script

0 个答案:

没有答案