ASP.NET MVC 3 - 验证问题

时间:2011-02-03 00:35:43

标签: asp.net-mvc-3

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

此对象包含CustomerVehicles类型的属性List<VehicleData>VehicleData有两个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上发布同样的问题后,我得到了一个解决方案。在帖子中,您将能够看到不显眼的验证框架中存在错误以及它如何处理下拉列表的验证。用户counsellorben在解释问题以及解决方案(包括示例代码)方面做得很好,这些解决方案将帮助其他人在将来避免此问题,或至少在Microsoft构建修复框架之前。

感谢大家的帮助。

4 个答案:

答案 0 :(得分:2)

我也遇到过这种明显大规模的关于客户端验证的监督,使用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添加到您的下拉列表中。 这将注入缺少的必要属性。您当然需要添加您可能需要的任何其他属性,例如您的类属性。

希望这有帮助。

答案 1 :(得分:2)

这是我发现的最简单的方法,只是在视图内的data-val-*-* HtmlAttributes中添加DropDownListFor属性。以下方法也适用于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" } })

我希望它可能有所帮助。最诚挚的问候!

答案 2 :(得分:0)

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

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

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

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

您究竟需要验证什么?

答案 3 :(得分:0)

我在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可能存在问题,但修复可以像在控制器中初始化对象一样简单。