0

I have one view in my ASP.net MVC 2.0 project, I want to list the list of employee that I create method GetProfileCustomer() in CustomerModels and GetTransaction() in TransactionModels.

How can I import two different of models in a single view?

4

1 回答 1

0

I'm fairly new to MVC as well, and I've struggled with the similar issues if I understand the question correctly.

I've found that you get much cleaner controller code if you design your ViewModels to be as close as possible to the data that the view is using. Your ViewModels can contain lists of other things including other model objects. Something like:

public class TransactionViewModel
{       
    public string dataelement1 { get; set; }
    public int dataelement2 { get; set; }
    //and so on...

    //The Lists
    public IList<Employee> EmpList { get; set; }
    public IList<OtherModel> SomethingElse { get; set; }
    //and so on...          
}

In your controller, you construct and initialize your ViewModel

something like...

TransactionViewModel TVM = new TransactionViewModel();
//assign basic attributes here..

//make a list
TVM.Emplist = (from blah in context select blah).ToList();

//send it to the view
return View(TVM);

Hope this helps and happy to hear any feedback...

于 2012-07-24T20:27:24.603 回答