zoukankan      html  css  js  c++  java
  • ASP.NET MVC4中@model使用多个类型实例的方法

     有时需要在ASP.NET MVC4的视图的@model中使用多个类型的实例,.NET Framework 4.0版本引入的System.Tuple类可以轻松满足这个需求。

            假设Person和Product是两个类型,如下是控制器代码。

    using System;
    using System.Web.Mvc;
    
    namespace Razor.Controllers
    {
        public class HomeController : Controller
        {
            Razor.Models.Product myProduct = new Models.Product { ProductID = 1, Name = "Book"};
            Razor.Models.Person myPerson = new Models.Person { PersonID = "1", Name = "Jack" };
            
            public ActionResult Index()
            {
                return View(Tuple.Create(myProduct,myPerson));  // 返回一个Tuple对象,Item1代表Product、Item2代表Person
            }
    
        }
    }
    @model Tuple<Razor.Models.Product, Razor.Models.Person>
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
        <div>
            @Model.Item1.Name
        </div>
    </body>
    </html>
  • 相关阅读:
    浅谈HTTP中Get与Post的区别
    js 执行完setTimeout再接着执行函数
    2017年书单
    js判断img是否存在
    md5
    GIF播放器
    java 集合(二)
    java 泛型
    抓包工具
    js计算地球两个经纬度之间的距离
  • 原文地址:https://www.cnblogs.com/mandalaluo/p/5010891.html
Copyright © 2011-2022 走看看