I am following Nerd Dinner tutorial (with EF) while creating a custom application but came across a problem in validating user input while adding a student to the database.
Lets say I have 3 entities
Student : StdID, StdName
Course: CourseID, CourseName
StdCourse: StdID,CourseID
Since the last table is a bridge table, the edmx file shows only two tables. On the add student page, I want to validate that the user has selected at least one course for the student from the courses list box, before adding the student.
I don't think you can do that directly,and in my mind,I think you can try to check whether a specific student instance has IEnumerable<Course> Course is null or its Count is larger than 0 or not,before you do inserting it into your db。
Reguards!
Marked as answer by ZahidBashir on Mar 03, 2012 09:26 AM
I spend the whole day searching to better understand how to insert/ validate properties involved in many to many relationship using EF. Still the ModelState.IsValid throws a validation error for the courses list box saying "The value '3,4' is invalid."
upon selecting the 3rd and fourth options in list box.
Following is my code
StudentFormViewModel.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using manytomany.Models;
namespace manytomany.ViewModels
{
public class StudentFormViewModel
{
public Student student { get; set; }
public List<Cours> courses { get; set; }
public StudentFormViewModel(Student student1, List<Cours> courses1)
{
student = student1;
courses = courses1;
}
}
}
HomeController:
public ActionResult Create()
{
var student = new Student();
var AllCourses = from c in entities.Courses
select c;
StudentFormViewModel viewModel = new StudentFormViewModel( student, AllCourses.ToList());
return View(viewModel);
}
[HttpPost]
public ActionResult Create(Student student)
{
var courses = Request.Form["student.Courses"];
if (courses == null)
{
//no course was selected
}
else
{
var courseids = courses.Split(',');
foreach (var courseid in courseids)
{
int id = int.Parse(courseid);
Cours course = entities.Courses.Where(c => c.CourseID == id).First();
student.Courses.Add(course);
}
if (ModelState.IsValid)
{
entities.Students.AddObject(student);
entities.SaveChanges();
}
}
var AllCourses = from c in entities.Courses
select c;
StudentFormViewModel viewModel = new StudentFormViewModel(student, AllCourses.ToList());
return View(viewModel);
}
ZahidBashir
Member
86 Points
57 Posts
MVC 2 Validating Many to Many Property
Mar 01, 2012 04:53 PM|LINK
Hi,
I am following Nerd Dinner tutorial (with EF) while creating a custom application but came across a problem in validating user input while adding a student to the database.
Lets say I have 3 entities
Student : StdID, StdName
Course: CourseID, CourseName
StdCourse: StdID,CourseID
Since the last table is a bridge table, the edmx file shows only two tables. On the add student page, I want to validate that the user has selected at least one course for the student from the courses list box, before adding the student.
How can I accomplish this.
Best Regards,
ZB
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: MVC 2 Validating Many to Many Property
Mar 03, 2012 12:20 AM|LINK
Hello:)
I don't think you can do that directly,and in my mind,I think you can try to check whether a specific student instance has IEnumerable<Course> Course is null or its Count is larger than 0 or not,before you do inserting it into your db。
Reguards!
ZahidBashir
Member
86 Points
57 Posts
Re: MVC 2 Validating Many to Many Property
Mar 03, 2012 08:22 AM|LINK
Thanks,
yeah i think you are right but before trying that I guess i should brush up my skills on how to insert data in many to many relastionship entities.
Could you please provide me with some links on the subject matter?
Regards,
ZB
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: MVC 2 Validating Many to Many Property
Mar 03, 2012 08:35 AM|LINK
For many to many insert,you can have a look at this:
http://thedatafarm.com/blog/data-access/inserting-many-to-many-relationships-in-ef-with-or-without-a-join-entity/
ZahidBashir
Member
86 Points
57 Posts
Re: MVC 2 Validating Many to Many Property
Mar 03, 2012 09:26 AM|LINK
Thanks alot :)
ZahidBashir
Member
86 Points
57 Posts
Re: MVC 2 Validating Many to Many Property
Mar 04, 2012 05:04 AM|LINK
Hi,
I spend the whole day searching to better understand how to insert/ validate properties involved in many to many relationship using EF. Still the ModelState.IsValid throws a validation error for the courses list box saying "The value '3,4' is invalid." upon selecting the 3rd and fourth options in list box.
Following is my code
StudentFormViewModel.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using manytomany.Models; namespace manytomany.ViewModels { public class StudentFormViewModel { public Student student { get; set; } public List<Cours> courses { get; set; } public StudentFormViewModel(Student student1, List<Cours> courses1) { student = student1; courses = courses1; } } }HomeController:
public ActionResult Create() { var student = new Student(); var AllCourses = from c in entities.Courses select c; StudentFormViewModel viewModel = new StudentFormViewModel( student, AllCourses.ToList()); return View(viewModel); } [HttpPost] public ActionResult Create(Student student) { var courses = Request.Form["student.Courses"]; if (courses == null) { //no course was selected } else { var courseids = courses.Split(','); foreach (var courseid in courseids) { int id = int.Parse(courseid); Cours course = entities.Courses.Where(c => c.CourseID == id).First(); student.Courses.Add(course); } if (ModelState.IsValid) { entities.Students.AddObject(student); entities.SaveChanges(); } } var AllCourses = from c in entities.Courses select c; StudentFormViewModel viewModel = new StudentFormViewModel(student, AllCourses.ToList()); return View(viewModel); }Create View:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<manytomany.ViewModels.StudentFormViewModel>" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Create</title> </head> <body> <% using (Html.BeginForm()) {%> <%: Html.ValidationSummary(true) %> <fieldset> <legend>Add Student</legend> <p><%: Html.LabelFor(m=>m.student.StudName) %> <%: Html.TextBoxFor(m=>m.student.StudName) %> </p> <p> <%: Html.LabelFor(m=>m.student.Courses) %> <%: Html.ListBoxFor(m=>m.student.Courses,new MultiSelectList(Model.courses,"CourseID","CourseName")) %> <%: Html.ValidationMessageFor(m=>m.student.Courses) %> </p> <p> <input type="submit" value="Create" /> </p> </fieldset> <% } %> </body> </html>Regards, ZB