简体   繁体   中英

How to use multiple models to one view

I have several data tables / Entities (APRICOT BANANA, LEMON) in the same EDMX and each table has multiple columns (Name, Quantity, Date, Sort)

In a view, I have a drop-down list (DropDownList) which contains ("A", "B" and "C")

What I want to do:

In my drop down list: When I selected the value A poster I down my drop down list (in the same view) Table data APRICOT

or

When I selected the value IB appears at the bottom of my combo (in the same order) the data from the table BANANA

or

When I selected the value C i appears at the bottom of my combo (in the same order) the data from the table BANANA

Are you can give me a sample code this class, the controller and the view? Sorry I'm a beginner! thank you

Thank you very much for your help

Jeremy

Create a new viewmodel class which will take all the data you need and pass that viewmodel to the view. You can then display the data from the viewmodel based on the dropdown list selected.

Fruit.cs

public class Fruit
{
    public string Name { get; set; }
    public int Quantity { get; set; }
}

FruitViewModel.cs

public class FruitViewModel
{
    public IEnumerable<Fruit> Fruits { get; set; }

    public FruitViewModel(IEnumerable<Fruit> fruit)
    {
        Fruits = fruits
    }
}

Controller.cs

public ActionResult FruitView()
{
   var fruits = //get your data here
   return View(new FruitViewModel(fruits));
}

View.cs

@model FruitViewModel

@Html.DropDownList("FruitTypeId", new SelectList(Model.Fruits , "Id", "Name"), "Select one")

Based on the selected Id you can display the value you need using Javascript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM