.net MVC中后台传前台的多种传值方式

PS:一个Home控制器里两个Action方法Index和Temp 分别对应两个视图;

  Index页面用于显示单个参数传值

  Temp页面用于显示多个参数参数

  (项目命名空间:MVC多种传值方式)

 两个类:

  Student :  public int stuId { get; set; } public string stuName { get; set; } public int stuAge { get; set; } public char stuSex { get; set; } public string stuEmail { get; set; }

  User :  public int uId { get; set; } public string uName { get; set; } public string uPwd { get; set; }

.net  MVC中后台传前台的多种传值方式

 

 

 后台Index方法(后台传递单个参数的几种方法):

.net  MVC中后台传前台的多种传值方式
public ActionResult Index()
        {
            #region ViewData传值
            //1)、传递对象
            ViewData["stu"] = new Student()
            {
                stuId = 1,
                stuName = "麻花筒",
                stuSex = '男',
                stuAge = 20,
                stuEmail = "25476342@qq.com"
            };
            //2)、传递标量值
            ViewData["flag"] = "能服于忍";
            #endregion

            #region ViewBag传值
            //1)、传递对象
            ViewBag.stu2 = new Student()
            {
                stuId = 2,
                stuName = "张三",
                stuSex = '男',
                stuAge = 18,
                stuEmail = "1472583699@qq.com"
            };
            //2)、传递标量值
            ViewBag.flag2 = "精忠报国";

            #endregion

            #region TempData传值
            //1)、传递对象
            TempData.Add("stu3", new Student()
            {
                stuId = 3,
                stuName = "李四",
                stuSex = '女',
                stuAge = 20,
                stuEmail = "1574258632@qq.com"
            });
            //2)、传递标量值
            TempData["flag3"] = "白昼流星";
            //3)、说明:使用Add方法和使用键值对作用是一样的;
            #endregion

            #region @model传值
            return View(new Student()
            {
                stuId = 4,
                stuName = "王五",
                stuSex = '男',
                stuAge = 16,
                stuEmail = "145236987@qq.com"
            });

            #endregion

        }
View Code

Index页面:

.net  MVC中后台传前台的多种传值方式
@using MVC多种传值方式.Models;

@model Student

@{
    Layout = null;
    //ViewData传值
    Student stu = ViewData["stu"] as Student;
    string flag = ViewData["flag"] as string;
    //ViewBag传值
    Student stu2 = ViewBag.stu2 as Student;
    string flag2 = ViewBag.flag2 as string;
    //TempData传值(通过键值接收)
    Student stu3 = TempData["stu3"] as Student;
    string flag3 = TempData["flag3"] as string;

}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Home-Index</title>
</head>
<body>
    @Html.ActionLink("多个对象传值页面","/Temp")
    @{
//ViewData传值
        <p>学号:@stu.stuId</p>
        <p>姓名:@stu.stuName</p>
        <p>年龄:@stu.stuAge</p>
        <p>性别:@stu.stuSex</p>
        <p>邮箱:@stu.stuEmail</p>
        <p>标志:@flag</p>
    }
    <hr />
    @{
//ViewBag传值
        <p>学号:@stu2.stuId</p>
        <p>姓名:@stu2.stuName</p>
        <p>年龄:@stu2.stuAge</p>
        <p>性别:@stu2.stuSex</p>
        <p>邮箱:@stu2.stuEmail</p>
        <p>标志:@flag2</p>
    }
    <hr />
    @{
//TempData传值
        <p>学号:@stu3.stuId</p>
        <p>姓名:@stu3.stuName</p>
        <p>年龄:@stu3.stuAge</p>
        <p>性别:@stu3.stuSex</p>
        <p>邮箱:@stu3.stuEmail</p>
        <p>标志:@flag3</p>
    }
    <hr />
    @{
//model传值
        <p>学号:@Model.stuId</p>
        <p>姓名:@Model.stuName</p>
        <p>年龄:@Model.stuAge</p>
        <p>性别:@Model.stuSex</p>
        <p>邮箱:@Model.stuEmail</p>
        <p>标志:无</p>
    }
    </body>
</html>
View Code

后台Temp方法(传递多个参数的几种方法):

.net  MVC中后台传前台的多种传值方式
public ActionResult Temp()
        {
            #region dynamic传值(匿名类型)

            dynamic dy = new ExpandoObject();
            dy.stu = new Student()
            {
                stuId = 5,
                stuName = "赵六",
                stuSex = '男',
                stuAge = 21,
                stuEmail = "147852369@qq.com"
            };
            dy.stu2 = new Student()
            {
                stuId = 6,
                stuName = "田七",
                stuSex = '男',
                stuAge = 12,
                stuEmail = "125874693@qq.com"
            };
            dy.flag = "天王盖地虎";
            dy.user = new User()
            {
                uId = 1,
                uName = "admin",
                uPwd = "123"
            };
            ViewData["dy"] = dy;

            #endregion

            #region Tuple传值(元数组)
            //说明:Tuple最多为八元数组 如果需传更多最后一个可用Tuple继续传
            var tuple = new Tuple<Student, Student, string, User, Tuple<Student, User>>(
                new Student()
                {
                    stuId = 7,
                    stuName = "阿帕奇",
                    stuSex = '男',
                    stuAge = 25,
                    stuEmail = "1472000@qq.com"
                }, new Student()
                {
                    stuId = 8,
                    stuName = "卡卡西",
                    stuSex = '男',
                    stuAge = 15,
                    stuEmail = "22356485@qq.com"
                },
                "宝塔镇河妖",
                 new User()
                 {
                     uId = 2,
                     uName = "administrator",
                     uPwd = "123456"
                 }, new Tuple<Student, User>(
                     new Student()
                     {
                         stuId = 9,
                         stuName = "帕卡丘",
                         stuSex = '男',
                         stuAge = 10,
                         stuEmail = "11245336@qq.com"
                     },
                     new User()
                     {
                         uId = 3,
                         uName = "account",
                         uPwd = "147258"
                     }));
            ViewData["tuple"] = tuple;

            #endregion

            //说明:无论是使用dynamic还是Tuple,最后将值传递到前台可以使用
            //ViewData、ViewBag、TempData、@model
            //任意一种方式,这里使用的是ViewData

            return View();
        }
View Code

Temp页面:

.net  MVC中后台传前台的多种传值方式
@using MVC多种传值方式.Models;

@{
    Layout = null;
    dynamic dy = ViewData["dy"] as dynamic;
    var tuple = ViewData["tuple"] as Tuple<Student, Student, string, User, Tuple<Student, User>>;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Home-Temp</title>
</head>
<body>
    @Html.ActionLink("传递单个对象页面","/Index")
    @{
//dynamic传值
        <p>学号:@dy.stu.stuId</p>
        <p>姓名:@dy.stu.stuName</p>
        <p>年龄:@dy.stu.stuAge</p>
        <p>性别:@dy.stu.stuSex</p>
        <p>邮箱:@dy.stu.stuEmail</p>

        <p>学号:@dy.stu2.stuId</p>
        <p>姓名:@dy.stu2.stuName</p>
        <p>年龄:@dy.stu2.stuAge</p>
        <p>性别:@dy.stu2.stuSex</p>
        <p>邮箱:@dy.stu2.stuEmail</p>

        <p>标志:@dy.flag</p>

        <p>用户id:@dy.user.uId</p>
        <p>用户名:@dy.user.uName</p>
        <p>用户密码:@dy.user.uPwd</p>

    }
    <hr />
    @{
        //tuple传值
        <p>学号:@tuple.Item1.stuId</p>
        <p>姓名:@tuple.Item1.stuName</p>
        <p>年龄:@tuple.Item1.stuAge</p>
        <p>性别:@tuple.Item1.stuSex</p>
        <p>邮箱:@tuple.Item1.stuEmail</p>

        <p>学号:@tuple.Item2.stuId</p>
        <p>姓名:@tuple.Item2.stuName</p>
        <p>年龄:@tuple.Item2.stuAge</p>
        <p>性别:@tuple.Item2.stuSex</p>
        <p>邮箱:@tuple.Item2.stuEmail</p>

        <p>标志:@tuple.Item3</p>

        <p>用户id:@tuple.Item4.uId</p>
        <p>用户名:@tuple.Item4.uName</p>
        <p>用户密码:@tuple.Item4.uPwd</p>

        <p>学号:@tuple.Item5.Item1.stuId</p>
        <p>姓名:@tuple.Item5.Item1.stuName</p>
        <p>年龄:@tuple.Item5.Item1.stuAge</p>
        <p>性别:@tuple.Item5.Item1.stuSex</p>
        <p>邮箱:@tuple.Item5.Item1.stuEmail</p>

        <p>用户id:@tuple.Item5.Item2.uId</p>
        <p>用户名:@tuple.Item5.Item2.uName</p>
        <p>用户密码:@tuple.Item5.Item2.uPwd</p>
    }
</body>
</html>
View Code

 

上一篇:使用idea实现SSM框架整合


下一篇:SQL server基本语法