views:

143

answers:

2

Hello, in new in ASP MVC im reading some examples and looking on internet, i think its not so hard once you jump into it.

im a webform programmer but i want to use MVC for internet applications and Webforms for Intranet Apps.

im looking the way of populate more than 1 table in MVC, because the method only allow me to return One ModelView or i dont know how to return more, i have to build the table in the controller and store into a ViewData and display the viewData in the page ??

[edited] sorry i think i dont express the idea, the point is that i want to populate 2 or more datatables in the page.

For example, if i Have a Customer and that Customer has 5 Contacts and 5 Address, i want to display the Customer Information and 2 Tables with Contacts and Address.

for each p in modelview { "" + p.Name + "" }

for each p2 in modelview2 { "" + p2.Product + "" }

[/Edit]

*sorry about my english :P

+2  A: 

ViewData is a dictionary; you can store multiple items in there, simply via:

ViewData["foo"] -= ...
ViewData["bar"] -= ...

You can then get these (separately) in the view. The other approach is to declare a type that encapsulates both properties, and use an instance of that type as the model (and a typed view). Personally, I prefer the simple key/cast approach.

Marc Gravell
For simply getting data to the view (list/show), I would agree. This approach isn't always the simplest when getting data from the view (new/update), though. Especially, if you already have a compound model, say User with related Contacts.
tvanfosson
foor and bar value will be the html code for a binded datatable?
jmpena
No; they are whatever data you put in them; the *view* creates html, not the controller
Marc Gravell
A: 

You can return more than one model, but it will require a compound model for the view and the use of prefixes when binding the controller parameter.

Say you have a View Model that is:

public class VM
{
   public FooClass Foo { get; set; }
   public BarClass Bar { get; set; }
}

Then in your controller, you would have

public ActionResult MyAction( [Bind(Prefix="Foo")]FooClass foo,
                             [Bind(Prefix="Bar")]BarClass bar )
{
   ...
}

And in your view you would use:

<%= Html.TextBox( "Foo.Value" ) %>
<%= Html.TextBox( "Bar.Name" ) %>
tvanfosson