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 9279579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:40:08+00:00 2026-06-18T17:40:08+00:00

I’m developing my first MVC4 app, after going through some tuts and a prototype

  • 0

I’m developing my first MVC4 app, after going through some tuts and a prototype I felt comfortable enough to dive in. I am using the code-first approach. I’m at the point where I am able to create a junction table using two class from my model to create a junction table via convention which is nice. Now I have a list of check-boxes that are associated with one end of this relationship showing up in the create view for the other, which is desired, the next thing I want to do is somehow validate and show an error message if at least one of the check-boxes are not checked.

My two models are Event and Category

    public class Event
{
    public int EventID { get; set; }
    [Required]
    public string Title { get; set; }
    public string Description { get; set; }
    public string URL { get; set; }
    public DateTime Published { get; set; }
    public DateTime? Modified { get; set; }
    public int Reputation { get; set; }
    public int CityID { get; set; }

    public virtual City City { get; set; }

    public virtual ICollection<Category> Categories { get; set; }
}

    public class Category
{
    public int CategoryID { get; set; }
    [Required]
    public string Name { get; set; }
    public DateTime Created { get; set; }
    public Boolean IsActive { get; set; }

    public virtual ICollection<Event> Events { get; set; }
}

This also creates an EventCategory table when run, as desired.

My Controllers are pretty simple

        public ActionResult Create()
    {
        ViewBag.CityID = new SelectList(db.Cities, "CityID", "Name");
        ViewBag.Categories = new MultiSelectList(db.Categories, "CategoryID", "Name");
        return View();
    }

    [HttpPost]
    public ActionResult Create(Event _event)
    {

        if (ModelState.IsValid && ModelState["Categories"] != null)
        {
            db.Events.Add(_event);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.Categories = new MultiSelectList(db.Categories, "CategoryID", "Name");
        ViewBag.CityID = new SelectList(db.Cities, "CityID", "Name", _event.CityID);
        return View(_event);
    }

And generating the check-boxes on my view is done by

            <div class="editor-label">
            @Html.LabelFor(model => model.Categories, "Categories")
        </div>
        <div class="editor-field">
            @foreach (SelectListItem c in ViewBag.Categories)
            {
                <input value="@c.Value" type="checkbox" name="Categories"/>@c.Text<br />
            }

        </div>

This allows for me to be able to reference a “Categories” index in the ModelStateDictionary which gives me the ID’s of the categories checked as a comma seperated list, I can deal with that, what I want to know is at this point how do I go about validating that at least one box is check in the categories?

I would like to do this is using data annotations but I’m not sure how this can be accomplished at this point since the check-box list seems like a hacky implementation. I know I could use js on client side and then some server side stuff to check this, however I’m not sure how I would go about displaying the error from the server side in this case, also I’m hoping there is a cleaner way to do it through the framework.

  • 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-06-18T17:40:10+00:00Added an answer on June 18, 2026 at 5:40 pm

    Here’s a custom validator for what you need.

    Blog Link

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am writing an app with both english and french support. The app requests
I am using Paperclip to handle profile photo uploads in my app. They upload
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker

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.