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

后端 未结 4 616
感情败类
感情败类 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:36

    It seems you can't pass anonymous types but if you just want the values of the type you might pass an enumerable of an object array to view.

    View:

    @model IEnumerable   
    
    @{
        ViewBag.Title = "Home Page";
    }
    
    
    @foreach (var item in Model) { }
    @item[0].ToString() @item[1].ToString()

    Controller:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Mvc;
    
        namespace ZZZZZ
        {
            public class HomeController : Controller
            {
                public ActionResult Index()
                {
    
                    List list = new List { new object[] { "test1", DateTime.Now, -12.3 } };
    
                    return View(list);
                }
    
    
            }
    
        }
    

提交回复
热议问题