Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 200
Ian Vink Avatar asked Sep 11 '13 22:09

Ian Vink


People also ask

How to put validation for Radio Button in ASP net?

To validate RadioButtonList in JavaScript <asp:RadioButtonList ID="RadioButtonList1" runat="server"> <asp:ListItem Value="Java">Java</asp:ListItem> <asp:ListItem Value="Dot Net">Dot Net</asp:ListItem> <asp:ListItem Value="JavaScript">JavaScript</asp:ListItem>

What is radio button control in asp net?

ASP.NET Web Forms RadioButton. It is an input control which is used to takes input from the user. It allows user to select a choice from the group of choices. To create RadioButton we can drag it from the toolbox of visual studio. This is a server side control and ASP.NET provides own tag to create it.

What is client side validation in MVC?

ASP.NET MVC client side validation is based on the jQuery validation plugin. It can be said that MVC's client-side validation is an opinionated version of how jQuery validation should work in an ASP.NET MVC project. Despite this, the underlying implementation is fully based on jQuery's.

Can radio button be validated?

We do not need to write any specific code to check the radio button. They can be checked once they are created or specified in HTML form.


1 Answers

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.

like image 109
asymptoticFault Avatar answered Oct 25 '22 05:10

asymptoticFault