ASP.NET MVC 3 - Validation Question

后端 未结 4 446
生来不讨喜
生来不讨喜 2020-12-19 10:09

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.

相关标签:
4条回答
  • 2020-12-19 10:37

    I had exactly the same problem with the field getting correctly validated in TextBoxFor but not in DropDownListFor.

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

    Since I had another DropDownListFor working on the same page, I knew that it wasn’t a generic DropDownListFor problem. I also have a complex model and parent object PaymentTO wasn’t initialized. When I set viewTO.PaymentTO = new PaymentTO(); in the Controller, the validation for the DropDownListFor started to work. So there is probably a problem with DropDownListFor, but the fix can be as simple as initializing the object in the controller.

    0 讨论(0)
  • 2020-12-19 10:39

    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!

    0 讨论(0)
  • 2020-12-19 10:40

    I too have come across this obviously massive oversight regarding client side validation with dropdownlists in MVC 3 and the best solution I can offer is to put the missing HMTL attributes in yourself.

    In your view model create a property like this.

    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;
            }
        }
    

    Then in your code, enter

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

    Just add the Model.CustomerVechicleAttributes as htmlAttributes to your dropdownlist. This will inject the necessary attributes that are missing. You will of course need to add any other attributes you may need like your class attribute.

    Hope this helps.

    0 讨论(0)
  • 2020-12-19 10:44

    you should try to add data annotations on your view model properties first so you could see the validation messages.

    you might find what you need here

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

    or create custom ones if needed.

    what exactly do you need to validate?

    0 讨论(0)
提交回复
热议问题