3

I'm very new to MVC so i apologize if i haven't explained my requirement properly/haven't used correct terminology.

I have a simple database with item and detail entities. They have a one-to-many relationship so Item has many Details.

I'd like to add records to both these tables from a single view.

So far my understanding is that if i add a create action for 'item' it will get an 'item' object in the HTTPPost request. So does that mean i have to create a separate action/view for details?

Or is 'partial-view' what i should use here? Can someone please give me an example. Really appreciate it. Thanks.

2 Answers 2

2

It sounds like you're using strongly typed views, and taking advantage of some MVC magic that auto-populates that type in action. I haven't used MVC3 yet, but in MVC2, you'd probably use UpdateModel to auto-populate your type from the formcollection. Whether MVC is doing some magic for your or not, you will always be sending information from your view to your controller (either POSTing or GETting with a url string) - you'll always be able to access the url string, or forms collection for the raw values.

That being said, you probably want to create a ViewModel - that is a model that fits the needs of your view. In your case it is just a compound model. This ViewModel, is just a plain class. It should be as easy as exposing each of your types as a property of this new class. If you do that, you'll don't forget to prefix your class/properties right, like: Model.Item.SomeProperty, Model.Detail.SomeOtherProperty If you don't, MVC probably won't be able to map values coming back correctly.

That should get you the data you need in your controller - then you just have to insert into the database.

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

Comments

1

This is the best reference for this technique:

http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

1 Comment

Yeah i saw that. I can use that to add details. But i'm not sure how the view should look like. Should it have like two partial-views or something (one for item and one for details)?

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.