Ian Vink
Ian Vink

Reputation: 68810

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?

Upvotes: 9

Views: 17361

Answers (1)

asymptoticFault
asymptoticFault

Reputation: 4529

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.

Upvotes: 16

Related Questions