简体   繁体   中英

How can I get multiple selections from ASP.NET CheckBoxs

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!

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.

OT: Don't perform excess actions:

return (from e in Enum.GetValues(typeof(ReferrerType))
        select e.ToDescription()).ToList();

or just

return Enum.GetValues(typeof(ReferrerType)).Select(e => e.ToDescription()).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM