简体   繁体   中英

Remove Unobtrusive validation for Particular Control in MVC3

I have implemented one simple MVC3 application in which I have used UnobtrusiveJavaScript validation. I have one drop-down in view which I don't want to validate it. event I have not put any validation in ViewModel.

ViewModel

public class TutorAddressViewModel
{

    public TutorAddressViewModel()
    {           
        AddressType = new List<SelectListItem>();
    }
    public List<SelectListItem> AddressType {get;set;}

    public byte TypeID { get; set; }

}

View

<table style="background: none; " class="addressDetail">
    <tr>
        <td>
            @Html.DropDownListFor(m => m.TypeID,Model.AddressType  , new { @Style = "Width:150px;" })
        </td>
</tr>
</table>

this Address Type dropdown I don't want to validate, when I have set UnobtrusiveJavaScriptEnabled this property false in web config it work fine means it not validate this control but when I have set it true it not allow me to POST the form when AddressType is empty.

So how can I remove the UnobtrusiveJava validation to particular control?

Setting UnobtrusiveJavaScript to false in your web config will disable your unobtrusive validations globally.

If you are using data annotations then you can decide your validation flow like which property you want to validate. Because data annotations and unobtrusive js validations work side by side in this way.

so my advise for you would be use data annotations structure to validate your modal properties.

//do not put an attribute if you do not want to validate it!
public List<SelectListItem> AddressType {get;set;}

public byte TypeID { get; set; }

if you don't want unobtrusive validation to fire on certain property, then do not use data annotation attribute over its property like in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM