Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum to Checkboxes in the Model C# MVC4

Now I was looking to make some validation for some checkbox fields in my model.

I want to create a unique rule that would require at least one checkbox to be true (or checked) in each category to make it valid. I have three different categories in this model.

I was told to approach this with enum as stated here.

I've looked into the situation and it seems a little over my head, because you basically utilize C# to customize your own rules.

Now these are the categories as mentioned in the hyperlink above:

//Disabilities

[Display(Name = "Learning Disabilities")]
public bool LD { get; set; }

[Display(Name = "Developmental Disabilities")]
public bool DD { get; set; }

[Display(Name = "AD/HD")]
public bool ADHD { get; set; }

[Display(Name = "Autism")]
public bool Autism { get; set; }

//Age Group

[Display(Name = "Child")]
public bool child { get; set; }

[Display(Name = "Youth")]
public bool youth { get; set; }

[Display(Name = "Adult")]
public bool adult { get; set; }

//Strategy Type

[Display(Name = "Academic")]
public bool academic { get; set; }

[Display(Name = "Behaviour")]
public bool behaviour { get; set; }

[Display(Name = "Communication")]
public bool communication { get; set; }

[Display(Name = "Social")]
public bool social { get; set; } 

Now to approach this I was told to use enum:

   public enum Age
    {
      [Display(Name="Child")
      Child,
      [Display(Name="Youth")
      Youth,
      [Display(Name="Adult")
      Adult
    }

^Do I throw this in the model still?

I know this goes into the model:

[Required]
public Age MyAge { get; set; }

After looking at several other examples I know that the above code is incomplete and I would also have to edit my view. As sad as it sounds, my education has not gone this far in programming so I apologize for my lack of understanding.

But if you could point me in the right direction so I can walk this golden brick road that would be much appreciated

Cheers.

like image 564
1011 1110 Avatar asked Jan 31 '14 04:01

1011 1110


1 Answers

Here is the small prototype I did for you with Enums and CheckBoxes and its validation.

Let your ENUM be -

public static class Data
{
    public enum BloodGroup
    {
        [Description("A+")]
        APositive,
        [Description("B+")]
        BPositive
    } 
}

Then construct your Enum model, which will hold the basic Checkbox properties -

public class EnumModel
{
    public Data.BloodGroup BloodGroup { get; set; }
    public bool IsSelected { get; set; }
}

Then construct Enum View Model based on Enum model, which basically have List of Enum Models -

public class EnumViewModel
{
    public List<EnumModel> CheckBoxItems { get; set; }
}

Then your Controller Index Action, will construct EnumViewModel and will bind it to Index View -

    public ActionResult Index()
    {
        EnumViewModel model = new EnumViewModel();
        model.CheckBoxItems = new List<EnumModel>();
        model.CheckBoxItems.Add(new EnumModel() { BloodGroup = Data.BloodGroup.APositive, IsSelected = false });
        model.CheckBoxItems.Add(new EnumModel() { BloodGroup = Data.BloodGroup.BPositive, IsSelected = false });
        return View(model);
    }

Index View will display all the checkboxes and will make a POST to Submit action on click of submit button -

@model MVC.Controllers.EnumViewModel

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@Html.ValidationSummary();

@using (Html.BeginForm("Submit", "Enum", FormMethod.Post))
{
    for (int i = 0; i < Model.CheckBoxItems.Count; i++)
    {
        @Html.LabelFor(m => m.CheckBoxItems[i].BloodGroup);
        @Html.CheckBoxFor(m => m.CheckBoxItems[i].IsSelected);
        @Html.HiddenFor(m => m.CheckBoxItems[i].BloodGroup);
    }

    <input type="submit" value="click"/>
}

In the Submit Action We check for the IsSelected properties of the Enum View Model, if there are none, then we return error to Index View.

    public ActionResult Submit(EnumViewModel model)
    {
        if (!model.CheckBoxItems.Where(p => p.IsSelected).Any())
        {
            ModelState.AddModelError("CheckBoxList", "Please select atleast one!!!");
            return View("Index",model);
        }

        return View();
    }

Output -

On Load -

enter image description here

When we do not select anything and submit the form -

enter image description here

like image 196
ramiramilu Avatar answered Oct 29 '22 12:10

ramiramilu