【问题标题】:ASP.NET MVC 3 - Validation QuestionASP.NET MVC 3 - 验证问题
【发布时间】:2011-02-03 00:35:42
【问题描述】:

大家晚上好,我有一个关于验证下拉列表值的问题。我有一个绑定到名为ReservationData 的视图模型类型的视图。

此对象包含 List<VehicleData> 类型的属性 CustomerVehiclesVehicleData 有两个int 属性VehicleMakeIdVehicleModelId

在我看来,我正在尝试遍历 CustomerVehicles 集合中的项目数量,并为每个项目显示两个下拉列表,一个车辆制造下拉列表和一个使用 DropDownListFor 的车型下拉列表。

当我尝试提交和验证时,我没有看到屏幕上显示任何验证错误。

以防万一您想知道我还为每个下拉列表添加了ValidationMessageFor。我不确定这是否与我的视图模型的结构及其复杂性以及控件需要如何命名或 ID 需要如何设置有关。任何帮助将不胜感激。

以下是对集合进行循环的代码:

@for (var i = 0; i < Model.CustomerVehicles.Count(); i++)
    {
        var vehicleNumber = i + 1;
        <div class="vehicle-selection-wrapper">
            <div class="content-container">
                <h3>
                    Vehicle @vehicleNumber</h3>
                <img class="vehicle-image" alt="manufacturer image" src="@Url.Content("~/Content/images/default-vehicle.gif")" /><br />
                @Html.LabelFor(m => m.CustomerVehicles[i].VehicleMakeId)
                @Html.DropDownListFor(m => m.CustomerVehicles[i].VehicleMakeId
            , new SelectList(Model.VehicleMakes, "Id", "Name")
                          , @UIDisplay.Dropdown_DefaultOption, new { @class = "long-field" })<br />
                @Html.ValidationMessageFor(m => m.CustomerVehicles[i].VehicleMakeId)<br />
                @Html.LabelFor(m => m.CustomerVehicles[i].VehicleModelId)
                @Html.DropDownListFor(m => m.CustomerVehicles[i].VehicleModelId
            , new SelectList(new List<CWR.Domain.VehicleModel>(), "Id", "Name")
                 , @UIDisplay.Dropdown_DefaultOption, new { @class = "long-field" })
                @Html.ValidationMessageFor(m => m.CustomerVehicles[i].VehicleModelId)
            </div>
        </div>
    }

好的,所以我还注意到,在生成的 HTML 中,生成的选择缺少与处理验证的元素相关联的 HTML5 data-val 属性。这是生成的 HTML

<select class="long-field" id="CustomerVehicles_0__VehicleMakeId"         name="CustomerVehicles[0].VehicleMakeId"><option value="">-- Select --</option>
</select><br />
<span class="field-validation-valid" data-valmsg-  for="CustomerVehicles[0].VehicleMakeId" data-valmsg-replace="true"></span><br />
<label for="CustomerVehicles_0__VehicleModelId">Model</label>
<select class="long-field" id="CustomerVehicles_0__VehicleModelId" name="CustomerVehicles[0].VehicleModelId"><option value="">-- Select --</option>
</select>
<span class="field-validation-valid" data-valmsg-for="CustomerVehicles[0].VehicleModelId" data-valmsg-replace="true"></span>

另外在我的VehicleData 类中,VehicleMakeIdVehicleModelId 属性用Required 属性装饰。

更新:

好的,所以我正在测试并注意到,如果我保持我的代码相同,除了我将Html.DropdownListFor 调用与Html.TextboxFor 调用交换,那么验证工作。这可能是什么原因造成的?会不会是不显眼的验证的框架错误?

更新:包含修复

所以在ASP.NET Forums 上发布了同样的问题后,我得到了解决方案。在这篇文章中,您将能够看到不显眼的验证框架中存在一个错误,以及它如何处理下拉列表的验证。用户顾问本很好地解释了问题以及解决方案(包括示例代码),这将有助于其他人在未来避免此问题,或者至少在 Microsoft 在框架中构建修复程序之前。

感谢大家的帮助。

【问题讨论】:

  • 您有如何在视图中使用此自定义助手的示例吗?谢谢。

标签: asp.net-mvc-3


【解决方案1】:

在 MVC 3 中使用下拉列表进行客户端验证时,我也遇到过这种明显的大规模疏忽,而我能提供的最佳解决方案是将缺失的 HMTL 属性放入您自己。

在您的视图模型中创建这样的属性。

public Dictionary<string, object> CustomerVechicleAttributes
    {
        get
        {
            Dictionary<string, object> d = new Dictionary<string, object>();
            d.Add("data-val", "true");
            d.Add("data-val-required", "Please select a Vechicle.");
            return d;
        }
    }

然后在你的代码中,输入

@Html.DropDownListFor(m => m.CustomerVehicles[i].VehicleMakeId
            , new SelectList(Model.VehicleMakes, "Id", "Name")
            , @UIDisplay.Dropdown_DefaultOption, 
            **Model.CustomerVechicleAttributes** })

只需将 Model.CustomerVechicleAttributes 作为 htmlAttributes 添加到您的下拉列表中。 这将注入缺少的必要属性。您当然需要添加您可能需要的任何其他属性,例如您的类属性。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    这是我发现的最简单的方法,只需在视图内的HtmlAttributesHtmlAttributes 中添加data-val-*-* 属性。以下方法也适用于 RemoteValidation,如果您不需要远程验证,只需删除包含 data-val-remote-* 的元素即可:

            @Html.DropDownListFor(m => m.yourlistID, (IEnumerable<SelectListItem>)ViewBag.YourListID, String.Empty, 
            new Dictionary<string, object>() { { "data-val", "true" }, 
            { "data-val-remote-url", "/Validation/yourremoteval" }, 
            { "data-val-remote-type", "POST" }, { "data-val-remote-additionalfield", "youradditionalfieldtovalidate" } })
    

    我希望它会有所帮助。最好的问候!

    【讨论】:

      【解决方案3】:

      您应该首先尝试在视图模型属性上添加数据注释,以便您可以看到验证消息。

      你可以在这里找到你需要的东西

      http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.aspx

      或根据需要创建自定义的。

      您究竟需要验证什么?

      【讨论】:

      • 只是想确认你是否遵循了关于如何验证视图的正确模式,比如添加 并包含必要的 javascripts?
      • @mreyeros - 您能否编辑您的帖子并将代码发布到那里,这里真的不可读。我目前只使用 ASP.NET MVC 2,对 ASP.NET MVC 3(RAZOR/ubostrusive ajax 等)的新东西不太熟悉。如果您可以清理您的帖子,希望其他人可以提供帮助。对不起
      【解决方案4】:

      在 TextBoxFor 中正确验证该字段但在 DropDownListFor 中没有正确验证时,我遇到了完全相同的问题。

      @Html.DropDownListFor(m => m.PaymentTO.CreditCardType,   Model.CreditCardTypeList, "Select Card Type", new { style = "width:150px;" }) 
      

      由于我在同一个页面上有另一个 DropDownListFor 工作,我知道这不是一个通用的 DropDownListFor 问题。我还有一个复杂的模型,并且父对象 PaymentTO 没有被初始化。当我设置 viewTO.PaymentTO = new PaymentTO();在 Controller 中,DropDownListFor 的验证开始工作。所以 DropDownListFor 可能存在问题,但修复可以像在控制器中初始化对象一样简单。

      【讨论】:

        猜你喜欢
        • 2012-03-30
        • 2011-07-31
        • 1970-01-01
        • 2011-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-28
        • 1970-01-01
        相关资源
        最近更新 更多