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.
Here’s a custom validator for what you need.
Blog Link