Skip to content

Initial check in of .NET Framework 4.x code ported to .NET 5.x

License

Notifications You must be signed in to change notification settings

aholland1973/CheckBoxList.Mvc.Core

Repository files navigation

CheckBoxList.Mvc.Core

NET 5.x (.NET Core) port of Stephen Zeng's CheckBoxList.Mvc for .NET Framework 4.x (https://github.com/stephenzeng/CheckBoxList.Mvc)

CheckBoxList.Mvc.Core

#####Extension methods for CheckBoxList in MVC

Overview

CheckBoxList.Mvc.Core aims to help you to code for mulitple selctions in ASP.NET MVC in the, well, MVC way. For example in the view @Html.TextBoxFor(m => m.Name) gives you a textbox input and other out-of-box benefits from ASP.NET MVC. With CheckBoxList.Mvc.Core you now can do the similar thing with the following methods:

  • @Html.CheckBoxList("name", list)
  • @Html.CheckBoxListFor(m => m.List)

list and m.List are type of IList<CheckBoxListItem>

  • @Html.EnumCheckBoxList("name", list)
  • @Html.EnumCheckBoxListFor(m => m.List)

list and m.Listare type of IList<TEnum>

Nuget package

You can search CheckBoxList.Mvc.Core in Nuget Package Manager, or run the following command in the Package Manager Console to install it.

PM> Install-Package CheckBoxList.Mvc.Core

How to use it

The demo project contains 4 simple examples of using the 4 extension methods which should be able to help you to start with.

Support for Enum list

It can be quite handy to use a list of enum types in your models for multiple choices, since the type itself already has all the available options, so that what contained in the model list is what options selected. EnumCheckBoxList() and EnumCheckBoxListFor() are for this purpose.

######Enum

public enum ExtremeSport
{
    [Description("Bungee jumping")]
    BungeeJumping = 0,

    [Description("Deep diving")]
    DeepDiving = 1,

    Kitesurfing = 2,

    Parachute = 3,
}

######ViewModel

public class ExtremeSportViewModel
{
    public ExtremeSportViewModel()
    {
        ExtremeSports = Enumerable.Empty<ExtremeSport>().ToList();
    }

    [Display(Name = "Do you do any of the extreme sports listed below in your spare time?")]
    public IList<ExtremeSport> ExtremeSports { get; set; } 
}

######Controller actions

public ActionResult ExampleEnumCheckBoxListFor()
{
    var viewModel = new ExtremeSportViewModel();
    viewModel.ExtremeSports.Add(ExtremeSport.RockClimbing);

    return View(viewModel);
}

[HttpPost]
public ActionResult ExampleEnumCheckBoxListFor(ExtremeSportViewModel viewModel)
{
    if (ModelState.IsValid)
    {
        var selectedOptions = viewModel.ExtremeSports.Select(e => e.GetEnumDisplayName());
        ViewBag.SelectedOptionsText = string.Join(", ", selectedOptions);
    }

    return View(viewModel);
}

######View

@using (Html.BeginForm())
{
    <div class="form-group">
        @Html.LabelFor(m => m.ExtremeSports)
        @Html.EnumCheckBoxListFor(m => m.ExtremeSports)
    </div>
    <div>
        <button type="submit" class="btn btn-default">Submit</button>
    </div>
}

About

Initial check in of .NET Framework 4.x code ported to .NET 5.x

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published