简体   繁体   中英

Comparing data from two models in one controller

I have two models:

public class Resort
{
    public int ID { get; set; }
    public String Name { get; set; }
    public int BlackDiamond { get; set; }
    public int BlueSquare { get; set; }
    public int GreenCircle { get; set; }
    public int TerrainPark { get; set; }
}

And

public class Input
{
    public int ID { get; set; }
    public string Name { get; set; }
    public bool GreenCircle { get; set; }
    public bool BlueSquare { get; set; }
    public bool BlackDiamond { get; set; }
    public bool TerrainPark { get; set; }
}

The idea here is to create a Controller that will allow me to have access to data from both Models as my logic will basically allow me to search through all Resorts looking for the Resort with the highest number of whichever experience level is preferred. As an example, let's say that I prefer BlackDiamonds, so I would want to search for the Resort with the highest number of BlackDiamonds. So I will need to know which experience level is preferred and which Resort has the highest number of that preferred experience level.

You could define a view model aggregating those 2 models:

public class MyViewModel
{
    public Input Input { get; set; }
    public Resort Resort { get; set; }
}

Now the controller actions could take/pass this view model from/to the views which will be strongly typed to the view model. Now you will have all the necessary data.

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