ASP.NET MVC 3 - 确认问题 [英] ASP.NET MVC 3 - Validation Question

查看:80
本文介绍了ASP.NET MVC 3 - 确认问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Good evening everyone I have a question regarding validation of drop-down list values. I have a view that is bound to a view model type called ReservationData.

这个对象包含类型的属性 CustomerVehicles 列表< VehicleData> VehicleData 有两个 INT 属性 VehicleMakeId VehicleModelId

This object contains a property CustomerVehicles of type List<VehicleData>. VehicleData has two int properties VehicleMakeId and VehicleModelId.

在我看来,我想遍历所有的 CustomerVehicles 集合中的项目数和每个显示两个下拉菜单,一个车辆制造下拉和车辆模型下拉使用 DropDownListFor

On my view I am trying to loop over the number of items in the CustomerVehicles collection and displaying two dropdowns for each, a vehicle make dropdown and a vehicle model dropdown using DropDownListFor.

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

When I try to submit and validate I do not see any validation errors displayed on the screen.

万一你想知道我添加了一个 ValidationMessageFor 每个下拉为好。我不知道这是我的观点模型的结构和其复杂性和问题的控制需要如何命名或IDS需要如何进行设置。任何帮助将大大AP preciated。

Just in case you are wondering I have added a ValidationMessageFor for each dropdown as well. I am not sure if this is an issue with the structure of my view model and its complexity and how the controls need to be named or how the ids need to be set. Any help would be greatly appreciated.

下面是code为遍历集合:

Here is the code for the looping over the collection:

@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的数据-VAL属性。下面是生成的HTML

Ok so I also noticed that in the generated HTML the selects that are generated are missing the HTML5 data-val attributes that are associated to elements to handle validation. Here is the generated 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 类的<​​code> VehicleMakeId VehicleModelId 属性与要求属性装饰。

Additionally in my VehicleData class the VehicleMakeId and VehicleModelId properties are decorated with a Required attribute.

好了,所以我在测试,发现如果我把我的code相同,除了我交换 Html.DropdownListFor HTML调用。然后TextboxFor 调用验证工作。可能是什么原因?难道是一个框架错误与不引人注目的验证?

Ok so I was testing and noticed that if I keep my code identical except I swap the Html.DropdownListFor calls with Html.TextboxFor calls then the validation works. What could be causing this? Could it be a framework bug with the unobtrusive validation?

所以在 ASP.NET论坛张贴同样的问题后,我能得到的溶液。在后,你将能够看到,有在不引人注目的验证框架的错误以及如何处理dropdownlists的验证。用户counsellorben确实在解释这个问题一个很好的工作,以及解决方案(包括样品code),这将帮助他人避免将来出现此问题,或者至少要等到微软建立在修复中的框架。

So after posting this same question on the ASP.NET Forums, I was able to get a solution. In the post you will be able to see that there is a bug in the unobtrusive validation framework and how it handles validation of dropdownlists. The user counsellorben does a good job in explaining the problem as well as a solution (including sample code) that will assist others in avoiding this issue in the future, or at least until Microsoft builds in a fix in to the framework.

谢谢大家的协助。

推荐答案

这是我发现做到这一点,只需添加simpliest方式数据-VAL - * - * 在属性 HtmlAttributes DropDownListFor 时,视图中。下面的方法与RemoteValidation工作过,如果你不需要远程验证,只需删除包含数据-VAL-远程元素 - *

This is the simpliest way I found to do it, just adding data-val-*-* attributes in HtmlAttributes of DropDownListFor, inside the view. The following method works with RemoteValidation too, if you do not need remote validation, simply remove the elements containing 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" } })

我希望这可以帮助。顺祝商祺!

I hope it may help. Best Regards!

这篇关于ASP.NET MVC 3 - 确认问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆