Razor view engine - How can I add Partial Views

前端 未结 2 867
误落风尘
误落风尘 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.

    0 讨论(0)
  • 2021-01-31 07:29

    You partial looks much like an editor template so you could include it as such (assuming of course that your partial is placed in the ~/views/controllername/EditorTemplates subfolder):

    @Html.EditorFor(model => model.SomePropertyOfTypeLocaleBaseModel)
    

    Or if this is not the case simply:

    @Html.Partial("nameOfPartial", Model)
    
    0 讨论(0)
提交回复
热议问题