开发者

ASP.net MVC.2 return multiple objects to strongly typed viewmodel

开发者 https://www.devze.com 2023-03-16 02:06 出处:网络
I am new to MVC and using the repository pattern in an attempt to select data containing two objects to return to a strongly typed viewmodel,

I am new to MVC and using the repository pattern in an attempt to select data containing two objects to return to a strongly typed viewmodel,

Im getting a bit stuck with what the best way to do this is,

The two tables are related by a customer id field, i have a repository interface set up, and a display template that is strongly typed to a viewmodel that contains properties for the Customer and a Customer Site object, all i need is to display a list of customer sites along with the relevant customer name from the customers table.

In the display template i have the following

<%= Html.DisplayFor(x => x.Customers.CustomerName) %>
<%= Html.DisplayFor(x => x.Customers.Site.AddressLine1) %>

I have this display template working but my model is empty.

Where im getting confused is how to define this data in the interface and repository, and how to return the data to my model, to simply return my list of customers i use this in my interface

IQueryable<Customer> Customers { get; }

Then a simple LINQ select.

But as this data will contain both customers and customer sites im unsure how to define this in the interface?

Also will a LINQ join be a suitable method to return this data to my viewmodel? something like

var Customers =

    from c in customers

    join cs in customerSites on c equals cs.Cus开发者_开发问答tomerId into ps

    from p in ps

    select new { Customer = c, cs.CustomerName };

UPDATE=========

This is the code i am using in the view model that is stronly typed to the display template,

public class CustomerViewModel
{
    public int Id { get; set; }
    public string CustomerName { get; set; }
    public string PrimaryContactName { get; set; }
    public SiteViewModel Site { get; set; }
}

Its how to populate the model in the repository/controller with both objects to display in a list that im struggling with.


You may have done soem of the following steps already so please ignore if you have..

1 creat a ViewModel folder in your solution.

2 Create a base view model .... might look like this ->

public class BaseViewModel
    {
        public PageProperties PageProperties { get; set; }
        public User User { get; set; }
    }

3 Setup a view model for your controller action maybe like so ->

public class ProjectVM : BaseViewModel
    {
        public ProjectPoco project { get; set; }

    }

4 In your controller get your data from your repositiory and pass it to an instance of your view model like this ->

var contextVM = new ProjectVM();

        contextVM.project = ObjectExtensions.Convert<ProjectPoco>(tbl.Single(id));
        contextVM.PageProperties = new PageProperties
                                       {
                                           Heading = contextVM.project.Name,
                                           SubHeading = "Details for" +

contextVM.project.Name }; return View(contextVM);

5 set your views model to be that of your view model ->

@model NerveCentre.ViewModels.ProjectVM

6 use your viewmodel to pull data out into your view ->

@Model.project.Description

A quick and rough description of passing data to your view via a view model. hope I didnt miss anything out.

As for the data.. looking at how you have the model (Customers.Site.AddressLine1) would it not just be possible to pass the Customers from your query to your view model?

So your viewmodel might look something like..

    public class SomeViewModel: BaseViewModel
    {
        public List<Customer> Customers { get; set; }

    }

If you let us know what you are using for data access then we might be able to help more with the specifics of getting the data out of your tables and into the format you want?

0

精彩评论

暂无评论...
验证码 换一张
取 消