Question How to Create Multiple CheckBox with Data Annotation Inside the View?

Elad

Member
Joined
Feb 15, 2020
Messages
13
Programming Experience
1-3
Hello!
I have two problems 1: How to create multiple checkboxes and require the user to select at least one checkbox?
2. How do I create within a 'View' using a CheckBoxFor loop with such a requirement? I did not find anything on the internet with such an example but only a standard input box of a check box running in a loop and created but without a test field. I am looking for some simple solution without the need for ajax or javascript but really c # directories only

model with Data annotation:
namespace AllStore.Models
{
    public class Store
    {
        int Id { set; get; }
        [Required(ErrorMessage = "Required field!")]
        [Display(Name = "Name")]
        string Name { set; get; }
        [Required(ErrorMessage = "Must choose at least one day!")]
        [Display(Name = "All days in week")]
        public IList<SelectListItem> DaysWork { get; set; }

        public Store()
        {
            DaysWork = new List<SelectListItem>()
            {
            new SelectListItem {Text = "Sunday", Value = "1"},
            new SelectListItem {Text = "Monday", Value = "2"},
            new SelectListItem {Text = "Tuesday", Value = "3"},
            new SelectListItem {Text = "Wednesday", Value = "4"},
            new SelectListItem {Text = "Thursday", Value = "5"},
            new SelectListItem {Text = "Friday", Value = "6"},
            new SelectListItem {Text = "Saturday", Value = "7"},
            };
        }

    }
}
}

and Controller is:
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace AllStore.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult AddWorker()
        {
            AllStore.Models.Store store = new AllStore.Models.Store();

            return View(store);
        }
        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}
And View I have no idea how to set up a checkbox in foreach:
@model AllStore.Models.Store

@{
    ViewBag.Title = "AddWorker";
}

<h2>AddWorker</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>Store</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,277
Location
Sydney, Australia
Programming Experience
10+
Hello!
I have two problems
Then you should have created two threads, each containing all and only the information relevant to one specific problem. Having multiple problems in one thread means more work for us to determine what information is relevant to what problem. It also means multiple conversations crossing over in the one thread and also that one post likely cannot be considered THE answer to THE problem. Please edit your post to cover one specific topic, then create a new thread for the other topic.
 
Last edited:
Top Bottom