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

2021-04-13 19:28

阅读:377

标签:多个参数   tun   http   action   用户密码   gif   png   out   head   

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; }

技术图片

 

 

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

技术图片技术图片
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页面:

技术图片技术图片
@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-Indextitle>
head>
body>
    @Html.ActionLink("多个对象传值页面","/Temp")
    @{
//ViewData传值
        p>学号:@stu.stuIdp>
        p>姓名:@stu.stuNamep>
        p>年龄:@stu.stuAgep>
        p>性别:@stu.stuSexp>
        p>邮箱:@stu.stuEmailp>
        p>标志:@flagp>
    }
    hr />
    @{
//ViewBag传值
        p>学号:@stu2.stuIdp>
        p>姓名:@stu2.stuNamep>
        p>年龄:@stu2.stuAgep>
        p>性别:@stu2.stuSexp>
        p>邮箱:@stu2.stuEmailp>
        p>标志:@flag2p>
    }
    hr />
    @{
//TempData传值
        p>学号:@stu3.stuIdp>
        p>姓名:@stu3.stuNamep>
        p>年龄:@stu3.stuAgep>
        p>性别:@stu3.stuSexp>
        p>邮箱:@stu3.stuEmailp>
        p>标志:@flag3p>
    }
    hr />
    @{
//model传值
        p>学号:@Model.stuIdp>
        p>姓名:@Model.stuNamep>
        p>年龄:@Model.stuAgep>
        p>性别:@Model.stuSexp>
        p>邮箱:@Model.stuEmailp>
        p>标志:无p>
    }
    body>
html>
View Code

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

技术图片技术图片
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 Tuplestring, User, Tuple>(
                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(
                     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页面:

技术图片技术图片
@using MVC多种传值方式.Models;

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

DOCTYPE html>

html>
head>
    meta name="viewport" content="width=device-width" />
    title>Home-Temptitle>
head>
body>
    @Html.ActionLink("传递单个对象页面","/Index")
    @{
//dynamic传值
        p>学号:@dy.stu.stuIdp>
        p>姓名:@dy.stu.stuNamep>
        p>年龄:@dy.stu.stuAgep>
        p>性别:@dy.stu.stuSexp>
        p>邮箱:@dy.stu.stuEmailp>

        p>学号:@dy.stu2.stuIdp>
        p>姓名:@dy.stu2.stuNamep>
        p>年龄:@dy.stu2.stuAgep>
        p>性别:@dy.stu2.stuSexp>
        p>邮箱:@dy.stu2.stuEmailp>

        p>标志:@dy.flagp>

        p>用户id:@dy.user.uIdp>
        p>用户名:@dy.user.uNamep>
        p>用户密码:@dy.user.uPwdp>

    }
    hr />
    @{
        //tuple传值
        p>学号:@tuple.Item1.stuIdp>
        p>姓名:@tuple.Item1.stuNamep>
        p>年龄:@tuple.Item1.stuAgep>
        p>性别:@tuple.Item1.stuSexp>
        p>邮箱:@tuple.Item1.stuEmailp>

        p>学号:@tuple.Item2.stuIdp>
        p>姓名:@tuple.Item2.stuNamep>
        p>年龄:@tuple.Item2.stuAgep>
        p>性别:@tuple.Item2.stuSexp>
        p>邮箱:@tuple.Item2.stuEmailp>

        p>标志:@tuple.Item3p>

        p>用户id:@tuple.Item4.uIdp>
        p>用户名:@tuple.Item4.uNamep>
        p>用户密码:@tuple.Item4.uPwdp>

        p>学号:@tuple.Item5.Item1.stuIdp>
        p>姓名:@tuple.Item5.Item1.stuNamep>
        p>年龄:@tuple.Item5.Item1.stuAgep>
        p>性别:@tuple.Item5.Item1.stuSexp>
        p>邮箱:@tuple.Item5.Item1.stuEmailp>

        p>用户id:@tuple.Item5.Item2.uIdp>
        p>用户名:@tuple.Item5.Item2.uNamep>
        p>用户密码:@tuple.Item5.Item2.uPwdp>
    }
body>
html>
View Code

 

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

标签:多个参数   tun   http   action   用户密码   gif   png   out   head   

原文地址:https://www.cnblogs.com/licm/p/12382116.html


评论


亲,登录后才可以留言!