.net MVC中后台传前台的多种传值方式
2021-04-13 19:28
标签:多个参数 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方法(后台传递单个参数的几种方法): Index页面: 后台Temp方法(传递多个参数的几种方法): Temp页面: .net MVC中后台传前台的多种传值方式 标签:多个参数 tun http action 用户密码 gif png out head 原文地址:https://www.cnblogs.com/licm/p/12382116.htmlpublic 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
}
@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>
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
@using MVC多种传值方式.Models;
@{
Layout = null;
dynamic dy = ViewData["dy"] as dynamic;
var tuple = ViewData["tuple"] as TupleStudent, Student, string, User, Tuple
文章标题:.net MVC中后台传前台的多种传值方式
文章链接:http://soscw.com/index.php/essay/75333.html