0

I have a page that is used to register people for training. The current page has three drop downs on it, one to select the person to register, one to select the course, and one to select the session that the course is offered. It is being requested that the page be modified to now have four groups of the Course and Session drop downs to allow for the user to register up to four classes for a person at a time. My problem arises on how to handle the form posting now. Before if a user tried to add a person to a course they were already registered for the Entity Framework model binding would catch the error and send the error message back to the form. I'm not sure how to implement this binding now that there are four instances to evaluate and save at the same time. I'm a newbie to MVC and EF so any help/direction would greatly be appreciated.

Edit: below is a piece of the orignial controller save function:

    var AddToCourse = new Session_Registrant()
{
    RegistrantID = RegistrantID,
    SessionID = Session1,
    RegistrantOrg = regOrg,
    RegistrantTitle = title,
    RegistedDate = DateTime.Now

};
//attempt to save
try
{
    if (ModelState.IsValid)
    {
        db.AddToSession_Registrant(AddToCourse);
        db.SaveChanges();
    }
}
catch (DataException error)
{
    if (error.InnerException != null)
    {
        if (error.InnerException.Message.Contains("UNIQUE KEY constraint"))
        {
            //adding custom error message to explain the failure
            ModelState.AddModelError("", "Error! Registrant has already been added to this course session.");
        }
    }
    else
    {
        ModelState.AddModelError("Error", error.Message);
    }
}
4
  • 2
    You'll probably get the help you desire if you can post your relevant code. Commented Sep 17, 2012 at 20:36
  • Thanks for the reply. I added some of the original save function above. Commented Sep 17, 2012 at 20:43
  • Are you one of the people, who refers to IE as "the internet" ? If not, then why are you referring to ASP.NET MVC framework as MVC (which is a language independent design pattern) ? Commented Sep 18, 2012 at 11:23
  • 2
    I didn't mean to cause any confusion, you're right I should have been more clear. Did you just comment to berate me or do you have any ideas? Commented Sep 18, 2012 at 13:19

1 Answer 1

1

I wonder if the problem is with the design of the function rather than the implementation. Should the problem be defined in terms of a one-to-many relationship between the registrant entity and the course/session entity.

I am also of the view that using exceptions as part of the design is bad practice and that this is a definition problem. Should you (your code) be asking questions like:

  • Is this a registered student
  • Does this student have a course already booked in this session
  • Is this student already registered on the course in a different session

In other words define your business rules, implement them and the problem should resolve itself into a SMOC (small matter of coding).

I am sorry if this response is not more directly helpful but I feel that this is the wrong starting point. Good luck anyway.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply. I am relatively new to programming so please be patient. You are correct that in the db there is a one to many relationship between a registrant and course/sessions. I'm not sure what you mean by "should my code be asking if the registrant has already booked this session". Is there another way that is a better practice? How else am I to prevent a user from being booked for the same course twice? Once again thank you for any help/direction.
What I was suggesting was first to define the business rules. One way to check if someone is already registered is to read from the database what the user already has registered for and check if there is a clash/conflict before writing the data rather than really on a primary key error. HTH
I see. I have been letting the constraints in the db do the check for me, I thought it was saving some coding. But it became my problem here when I try to use it with multiple inserts. I will implement a few queries in the beginning of my save function to check for whether a registrant has already been registered or not before saving. Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.