在Action中传递多个Model对象给View以及在View页面上获取对象属性值

方法1.在Action中使用ViewBag、ViewData、TempData传递视图Model给
某个视图:

实体类User:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVC_Project05.Models
{
    public class User
    {
        public int UserId { get; set; }
        public string UserName { get; set; }
        public int Age { get; set;}
        public string Telephone { get; set; }
    }
}

Demo:
动作方法

public ActionResult GetCurrentTime() {
	User user = new User() {
    UserId=10101,
    UserName="paul gasol",
    Age=36,
    Telephone="0568-5598565-9612"
};
//注意: View(string viewName) 传入的时候切记如果是字符串类型,一定是视图名称

ViewBag.Message = "you are my destinty!";
ViewBag.Date = DateTime.Now;
ViewBag.User = user;
return View();
//return View(DateTime.Now);
}

在View上获取对象属性值:

@*
    model关键字指定视图的模型类型

    强类型视图:实际上就是给视图声明了一个类型参数,
    渲染视图时必须强制传入对应的类型对象(在对应的动作方法里,必须在View()方法里传入Model),
    否则将引发异常
*@

@*@model DateTime*@
@*引入Model类所在命名空间*@
@using MVC_Project05.Models
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>获取当前时间</title>
</head>
<body>
    <div>
        @*<p>当前时间:@Model.ToShortTimeString()</p>*@
        @* 
        当接收从Acton传递来的参数(非Model传参方式)
        使用ViewBag、ViewData、TempData
        可以在Rasor表达式中先获取传递的对象,再获取其属性,
        或调用其方法获取一些值
        *@
        @{
            DateTime date = @ViewBag.Date;
            string currentDate = date.ToShortTimeString();
            User user = @ViewBag.User;
            string userName = user.UserName;
            int userId = user.UserId;
            int age = user.Age;
            string telephone = user.Telephone;
        }

        <p>Current Time:@currentDate</p>

        <p>用户个人信息:</p>
        <p>用户编号:@userId</p>
        <p>用户姓名:@userName</p>
        <p>年龄:@age</p>
        <p>电话号码:@telephone</p>
    </div>
</body>
</html>

Result:
在这里插入图片描述

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

Tuple类

实体类Contact:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MVC_Project05.Models
{
    public class Contact
    {
        public string UserName { get; set;}
        public int Age { get; set;}
        public string Gender { get; set;}
        public string Address { get; set; }
    }
}

动作方法:

        public ActionResult GetCurrentTime() {

            User user = new User() {
                UserId=10101,
                UserName="paul gasol",
                Age=36,
                Telephone="0568-5598565-9612"
            };

            Contact contact = new Contact() {
                 UserName="paul gasol",
                 Age=36,
                 Gender="男",
                 Address="USA"
            };
            return View(Tuple.Create(user,contact));
        }

注意:Create()方法最多接收8个参数(最多能传递8个Model对象),而且是一个泛型方法(string、object、数组、集合、自定义类型都可以传入)

在View获取对象属性:

@*
    model关键字指定视图的模型类型

    强类型视图:实际上就是给视图声明了一个类型参数,
    渲染视图时必须强制传入对应的类型对象(在对应的动作方法里,必须在View()方法里传入Model),
    否则将引发异常
*@
@*引入Model类所在命名空间*@
@using MVC_Project05.Models

@*接收Tuple传递过来的Model对象*@
@model Tuple<User,Contact>
@{
    Layout = null;
}
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>获取当前时间</title>
</head>
<body>
    <div>
		//获取Tuple传递方式的对象数据
		
       <p>用户个人信息:</p>
        <p>用户编号:@Model.Item1.UserId</p>
        <p>用户姓名:@Model.Item1.UserName</p>
        <p>年龄:@Model.Item1.Age</p>
        <p>电话号码:@Model.Item1.Telephone</p>
        <br /><br />
        <p>联系人信息:</p>
        <p>联系人姓名:@Model.Item2.UserName</p>
        <p>性别:@Model.Item2.Gender</p>
        <p>年龄:@Model.Item2.Age</p>
        <p>家庭住址:@Model.Item2.Address</p>
    </div>
</body>
</html>

Result:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值