views:

64

answers:

1

I have multiple tables that are all linked back to a central table with foreign keys. I want to be able to create a new record in table 2, but I'm having trouble because I don't know how to create a new instance of table 2's record while referencing the ID of the record it will be tied to.

EXAMPLE:

Database: Collection

Table: Collection Field 1: id Field 2: name

Table: Book Field 1: collectionId Field 2: id Field 3: name

Now, I don't want to be able to create a book without setting it's collectionID, but I can't figure out how this should be divided in the controllers/views.

Should Book have a controller separate from Collection, or should the Collection controller have a createBook method, separate from it's own create method?

I want to call the createBook method (from it's own controller, or the Collection controller) from the Collection Details view.

When I invoke the create method of Book, how do I create a new Book that is instantiated with the collectionId set from the details view of the Collection item that was listed in the details view?

I should point out, I'm using the entity framework for my model, and I'm definitely new to this.

Thanks for any help

+1  A: 

Try not to create/seperate controllers based on your entity model, create/seperate them based on the UI and URL Routes.

I'm guessing you have a View called "CreateBook", which is (hopefully) bound to a strongly-typed "Book" entity.

When you submit the form, you should have a controller method with [HttpPost].

Then you can do this to submit the book:

string bookName = Request.Form["Name"];
int collectionId = Request.Form["CollectionID"];

or pass it through as a strongly-typed model:

public ActionResult CreateBook(Book book) { }

Then you do this:

Book newBook = new Book (book.Name, book.CollectionID);
someDal.CreateBook(newBook);

When you import your model into the EDMX, make sure you tick "Include Foreign Keys in the Model".

What this does is add the "CollectionID" FK to the Book entity, allowing you to bind to this and have access to this property on each book model.

There is a really good article here on CRUD operations with Strongly Typed ASP.NET MVC Views, should put you on the right track.

HTH.

RPM1984
Thank you very much for the help!
Andy Poquette
No worries mate. :)
RPM1984
So, I understand this example, but if I'm trying to automatically insert the correct collection id, without that element appearing on the form, how would I do that? So, if collectionDetailsView has a link to createNewBook, how do I tell the [HttpPost]createNewBook method that the collectionID for it is the one that the link came from?
Andy Poquette
A common problem. :) A way to do this is to store the CollectionID as a hidden field in the Form - using `<%= Html.Hidden("CollectionID", Model.CollectionID) %>`
RPM1984
if you're displaying multiple books (ie in e `foreach`) then you'll need to put that hidden field inside that loop, and create a dynamic ID.
RPM1984
an alternative to the hidden field way is to pass the ID as a parameter to your controller, in addition to the model. `ActionResult CreateBook(int categoryId , Book newBook){}`
RPM1984