Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3958070
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:34:01+00:00 2026-05-20T02:34:01+00:00

This seems like it should be prettty easy – but I just can’t get

  • 0

This seems like it should be prettty easy – but I just can’t get it to work!

I have an enum in my model, which I want to display as a list of checkboxes. The user can select multiple checkboxes, and I want to save this in the database.

So the enum is like so (approx 20 elements unabridged):

public enum ReferrerType
{
    [Description("No Data")]
    NoData = 9999,
    [Description("Father or Mother")]
    Parents = 1,
    [Description("Brother or Sister")]
    Sibling = 2,
    [Description("Other")]
    Other = 10
}

Whereby the Description is what is shown on the UI, and the numeric value is what is to be saved in the database. The numbers have to remain as listed, as they go directly into a stats package.

I then defined a Referrer class:

public class Referrer
{
    public virtual Guid Id { get; private set; }
    public virtual ReferrerType{ get; set; }
}

I realise this might be an odd (anti)pattern. I developed it in haste, and am repenting at leisure. Any advice on improving this model would also be much appreciated!

My controller sets up the list:

private static IList<string> GenerateReferrerList()
{
    var values = from ReferrerType e in Enum.GetValues(typeof(ReferrerType))
                 select new { Name = e.ToDescription() };

    return values.Select(x => x.Name).ToList();
}

And I use it in my View like this:

<div class="radio-boolean form-field" id="Referrers">
      <p class="form-field-attribute"> <span class="label">Referred By </span> </p>
      <% for (var i = 0; i < ((IList<string>)ViewData["ReferrerList"]).Count; i++)
      { %>
           <p class="form-field-value">
           <%= Html.CheckBox(string.Format("Referrers[{0}].Type", i) ) %>
           <label for="Referrers"> <%= ((IList<string>)ViewData["ReferrerList"])[i]%></label>
       </p>
</div>

And it doesn’t work! I guess I’m missing something obvious, but I can’t work out what. There are no errors – just an empty database table where referrers should be…

As always, any help much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T02:34:02+00:00Added an answer on May 20, 2026 at 2:34 am

    Let’s take a moment and see what do we need here. We need to show a form which will contain multiple checkboxes (one for each value of the enum) and an associated label (this label should come from the Description attribute use on the enum). When this form is submitted we want to fetch all the values that the use has checked.

    So as always once we have clear definition of what we are trying to do we introduce our view model:

    public class MyViewModel
    {
        public bool IsChecked { get; set; }
        public ReferrerType ReferrerType { get; set; }
        public string Text { get; set; }
    }
    

    Then we write a controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = Enum.GetValues(typeof(ReferrerType)).Cast<ReferrerType>().Select(x => new MyViewModel
            {
                ReferrerType = x,
                Text = x.ToDescription() // I guess that's an extension method you wrote
            });
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(IEnumerable<MyViewModel> model)
        {
            ...
        }
    }
    

    And finally a strongly typed view corresponding to the Index action of our controller (~/Views/Home/Index.aspx):

    <% using (Html.BeginForm()) { %>
        @Html.EditorForModel()
        <input type="submit" value="OK" />
    <% } %>
    

    and the last part is the corresponding editor template (~/Views/Home/EditorTemplates/MyViewModel.ascx):

    <%@ Control 
        Language="C#" 
        Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.MyViewModel>" %>
    <%= Html.CheckBoxFor(x => x.IsChecked) %>
    <%= Html.HiddenFor(x => x.ReferrerType) %>
    <label><%: Model.Text %></label>
    

    Now when this form is submitted inside the POST index action you would get a list of all enums with a corresponding boolean value indicating whether the user checked it or not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First check out this code. I seems like it should work for me, but
It seems like this should be straightforward but I'm boggling. I've got my listview
Anybody have a slicker way to do this? Seems like it should be easier
This seems like a pretty softball question, but I always have a hard time
This seems like a simple question, but I can't find it with the Stack
This seems like such a trivial problem, but I can't seem to pin how
It seems like there should be something shorter than this: private string LoadFromFile(string path)
This seems like a silly question but I would really like your comments and
This seems like perhaps a naive question, but I got into a discussion with
Maybe I'm having a brain fart or something because it seems like this should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.