2

I am displaying radio buttons using enum class.

public enum RegisteredBy
    {
        [Display(Name = "Customer", Order = 0)]
        H,
        [Display(Name = "Dealer/Contractor", Order = 1)]
        S,
    }

When i am rendering this on my view and on submit I am not selected any radio button. Even though it is taking "H" as default value. So that it is not showing any validation message.

  @using ConsumerProductRegistration.Models;
    @using ProductRegistration.Models.Enums;
    @model ProductRegistration.Models.Registration

        @Html.RadioButtonFor(m => m.RegisteredBy, RegisteredBy.H, new { id = "RegisteredByCustomer" })
        @Html.Label("Customer")<br />
        @Html.RadioButtonFor(m => m.RegisteredBy, RegisteredBy.S, new { id = "RegisteredByDealer" })
        @Html.Label("Dealer/Contractor")
 @Html.ValidationMessageFor(m => m.RegisteredBy)

In Model:

public class Registration
    {
        [Required(ErrorMessage = "Select at least one option")]
        [Display(Name = "Registered by*")]
        public RegisteredBy RegisteredBy { get; set; }
}

In view:

public ActionResult CustomerInfo(Registration registration)
        {
            return View(registration);
        }

please suggest me.If user does not select we should show the error message.

Satya
  • 185
  • 1
  • 2
  • 16
  • 1
    Make the property nullable - `public RegisteredBy? RegisteredBy { get; set; }` –  Mar 11 '15 at 06:58
  • Yes it is working... How can i remove the borders of controls(red color) while for errors. – Satya Mar 11 '15 at 07:07
  • Not sure I understand, You have marked it as `[Required]` so if you don't select an option it will have a validation error. Why do you not want to indicate the error? –  Mar 11 '15 at 07:11
  • I want to show error message only. But by default that radio button control borders are highlighted with red color. I don't want to show that red color borders for the controls. Eg: [link](http://www.codeproject.com/KB/aspnet/639695/Razor-View-Engine-in-MVC-validation1.png) – Satya Mar 11 '15 at 07:25
  • That's just a matter of modifying the .css file. –  Mar 11 '15 at 07:27
  • yes i have modified in .css like below. `.input-validation-error { /*border: 1px solid #ff0000;*/ /*background-color: #ffeeee;*/ }` Thank you... – Satya Mar 11 '15 at 07:28
  • I Have one question. How to navigate from 1 view to another views with passing data. The pages have Previous,Next buttons. When ever user clicks Previous buttons we have to show the respective page with pre loaded data. I have used sessions for showing. But can you suggest me the good approach for navigating from one screen to multiple screen. – Satya Mar 11 '15 at 07:34
  • Cant really comment without seeing the relevant code (and that would need to be a new question). Personally I never use `Session` - I always get the data again from the database. –  Mar 11 '15 at 07:38
  • I have posted please click this link. [link](http://stackoverflow.com/questions/28984407/persist-data-from-one-view-to-multiple-views-having-next-previous-buttons?noredirect=1#comment46217814_28984407)[link] – Satya Mar 11 '15 at 11:02
  • @SatyaPratap Please upvote the anwser if it worked for you so that others would prefer it – Reyan Chougle Sep 06 '17 at 18:00

1 Answers1

2

The default underlying type of the enumeration elements is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.

When you are not selecting anything and posting the form, the default value 0 is automatically getting set (default value of integer).

In this case, you can make your property nullable with [Required] attribute which sends null as value when nothing is selected. And as it is decorated with [Required] attribute, it will give you required field validation error.

[Required]
public RegisteredBy? RegisteredBy { get; set; }
Reyan Chougle
  • 4,917
  • 2
  • 30
  • 57