简体   繁体   中英

ASP.NET MVC 4 Required Validation client side on Radio Buttons

I have a series of radio buttons, one must be selected whent he user clicks the submit button:

@Html.RadioButtonFor(o => o.EquityOrder.OrderAction, EnumOrderAction.B, new {id = "actionBuy"})
@Html.RadioButtonFor(o => o.EquityOrder.OrderAction, EnumOrderAction.S, new {id = "actionSell"})

Is there a way to ensure that the form doesn't submit and the @Html.ValidationSummary() is populated with a "OrderAction must be selected" string?

Just tag the OrderAction property in your model with this:

[Required(ErrorMessage = "OrderAction must be selected")]

I assume you have client validation enabled in either the web.config or the view and you have included the jquery validation file.

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