Razor view engine - How can I add Partial Views

前端 未结 2 866
误落风尘
误落风尘 2021-01-31 06:58

I was wondering what, if it is possible, is the best way to render a partial using the new razor view engine. I understand this is something that wasn\'t finished completely by

2条回答
  •  清酒与你
    2021-01-31 07:16

    If you don't want to duplicate code, and like me you just want to show stats, in your view model, you could just pass in the models you want to get data from like so:

    public class GameViewModel
    {
        public virtual Ship Ship { get; set; }
        public virtual GamePlayer GamePlayer { get; set; }     
    }
    

    Then, in your controller just run your queries on the respective models, pass them to the view model and return it, example:

    GameViewModel PlayerStats = new GameViewModel();
    
    GamePlayer currentPlayer = (from c in db.GamePlayer [more queries]).FirstOrDefault();
    

    [code to check if results]

    //pass current player into custom view model
    PlayerStats.GamePlayer = currentPlayer;
    

    Like I said, you should only really do this if you want to display stats from the relevant tables, and there's no other part of the CRUD process happening, for security reasons other people have mentioned above.

提交回复
热议问题