razor view with anonymous type model class. It is possible?

后端 未结 4 615
感情败类
感情败类 2020-12-03 05:08

I want to create a view using razor template, but I do not want to write a class for model, because in many views i will have many queries which will be returning diferent m

4条回答
  •  心在旅途
    2020-12-03 05:42

    The simplest solution if you are using C# 7.0+ (introduced in Visual Studio 2017+) is to use a tuple rather than an anonymous type.

    Razor View: "_MyTupledView.cshtml"

    @model (int Id, string Message)
    
    

    Id: @Model.Id

    Id: @Model.Message

    Then when you bind this view, you just send a tuple:

    var id = 123;
    var message = "Tuples are great!";
    return View("_MyTupledView", (id, message))
    

提交回复
热议问题