开发者

ASP.Net MVC View returned, but how can I show additional information?

I have a SQL database with has the following: Customer, Item, Clothing and Food.

Item holds a key to Clothing or Food. Item also holds a key to Customer. Therefore a customer can have an item, which may be of food or clothing.

I am us开发者_Python百科ing ADO.Net Entity Framework and have this generated automatically.

I currently have the following set-up: A person may enter their ID on the webpage and this is sent via a form post where the controller picks it up and queries the database using LINQ to get the customer. The customer view (details) is then returned. I can now see all the customer details etc.

However, what I want is to be able to see the items the customer has, the different food items and clothing items, but I am unsure how to do this. I also want to be able to allow the user to edit one field of the clothes and food items tables. Any idea how I would implement this?

Here is an ActionResult in my CustomerController:

        public ActionResult Details(int id)
    {
        var cust = (from c in dataModel.Customers
                    where (c.MembershipID == id)
                    select c).First();
        return View(cust);
    }

I can also write cust.Items which is the entity which I want to display in the view with the customer (their items). How would I display this in the view also?

Hopefully this makes it a little more clear on what I am trying to achieve and how.

Thanks.


Using Entity Framework, if you're tables are linked properly with the right foreign keys and all that then your Customer entity should have a property that is a collection of Items.

You could also create your own strongly typed ViewModel that has a field for Customer and implement your own properties for Clothing and Food and populate those with another query.

This question was asked last night but its similar. The guy in the question wanted information to populate a dropdown passed in. You want something similar, not for a dropdown, but to fill in textboxes to edit. How to properly populate drop downs from ViewData in controller on multiple views in ASP.NET MVC

To create a ViewModel start by creating a new class and name it CustomerAndItemsViewModel, for example.

public class CustomerAndItemsViewModel
{
    public Customer Customer { get; set; }
    public IQueryable<Items> Items { get; set; }        
}


public ActionResult Details(int id)
{
    var cust = (from c in dataModel.Customers
                where (c.MembershipID == id)
                select c).First();
    var items = (from i in dataModel.Items
                where (i.MembershipID == cust.MembershipID)
                select i;
    return View(new CustomerAndItemsViewModel { Customer = cust, Items = items });
}

And don't forget that you will no longer be passing a Customer to your view. So you need to change the line at the top to something like:

@model Your.Path.To.CustomerAndItemsViewModel


Typically, if you want to pass back information that is not contained in just one of your entities, you have to create a class that encompasses more than one object. So, if you want a page that displays your customer information, and all their items (which they can then edit), you would need to have a the controller action pass back a "CustomerAndItems" object (or something similarly named). This object would hold a reference to the Customer as well as a collection of their Items. (You build the CustomerAndItems object within your Action.)

Then, your view would be strongly typed to CustomerAndItems, and you can then display each piece of information as you normally would.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜