Asp.net mvc 4 mvc 4.5 many to one create using http post

Asp.net mvc 4 mvc 4.5 many to one create using http post,asp.net-mvc-4,Asp.net Mvc 4,I have a problem with MVC displaying list of many to one objects using an http post. How do I perform a db.SaveChanges() so that it includes each claw from my form submit? example primary entity: Cats table has Cats: id, and cat name only. many to one cat entity (notice CatId): Paws: Paw Number, claw1, claw2, claw3, claw4, claw5, and finally CatId each claw number represents a sharpness index. On my main form I have only a single cat and have all 4 paws and its sharpness index listed

I have a problem with MVC displaying list of many to one objects using an http post. How do I perform a

db.SaveChanges()
so that it includes each claw from my form submit?

example primary entity: Cats table has

Cats: id, and cat name only.

many to one cat entity (notice CatId):

Paws: Paw Number, claw1, claw2, claw3, claw4, claw5, and finally CatId 

each claw number represents a sharpness index.

On my main form I have only a single cat and have all 4 paws and its sharpness index listed out.

Now if I want to save each of the 4 paws and claws for this one cat how do I get save

db.SaveChanges()
if there is no it pointing to
Paws
in the
Cats
table. This seems so basic but haven't been able to tweak it yet. I've even set the
@Html.ForEditor
control to have an id of
Paws
and even
Paws.Claws
or
Paws.Claw
doesn't seem to work.

I'm trying to use MVC 4.5 for this.


#1

Many-to-one relationships are usually "managed" on the many side - in your case in the Paw entity. Setting the owning Cat (or its ID) for each Paw should work. The paws might not necessarily be visible from the Cat side of the relationship until you reload it from the context though - I'm not sure whether EF does this automagically.

#2

Also, consider improving your question a little. Use inline code formatting, and instead of an ad-hoc description of your entities, use C# classes or SQL. Same for what you've tried - use code, simplified as much as possible, instead of prose.

#3

So I should db.SaveChanges first, then some how retrieve that Cat's Id? How would do I get the Cats Id after doing a db.SaveChanges? Also how do I get each of the entity for each Paw after I do that and then do another db.SaveChanges? thx

#4

Is your issue that you can't get the model binding to work in your post action or that you aren't sure how to structure your entities in your model (I'm assuming EF from db.SaveChanges())?

#5

I think the structure is correct, but the problem is I just can't get the binding to work since the Cat doesn't have a primary key to all four paws. Only each Paw has a foreign key to the cat.