传值

 

四种传值方式
ViewBag.Content = "这是内容";//一般储存不重要的数据
ViewData["Age"] = 40;//一般储存不重要的数据

TempData["hello word"] = "word";//全局变量,同Sesson,但使用一次后就自动清空

Model

 

return View("ShowData2",masterName: "_Layout1", new student()
{
Id=1,
Name= "张三",
Age=12
});

ShowData2跳转地址,_Layout1使用的布局页,new student()模型类

 

视图页面加上@model WebApplication1.Models.student代码是弱类型变成强类型,弱类型没有提示

例如

@Model.Id
@Model.Name
@Model.Age

不提示

 

public ActionResult Index(string name)
{
// Request.QueryString["name"]
//Request.Form["name"]
return View(name);
}

string name 参数传值

Request.QueryString["name"]   get传值
Request.Form["name"]              post传值

public ActionResult Login(Models.LoginViweModel model){}    model传值    

 

页面进行非空,限制长度等验证要加ModelState.IsValid)

 

 [DataType(DataType.Password)] 密码格式

 [Required,StringLength(maximumLength:10,MinimumLength =2)]不允许为空,且最短2,最长10

 [DisplayName("电子邮箱")]邮箱格式

 

posted @ 2021-11-24 21:44  浅夏y  阅读(42)  评论(0编辑  收藏  举报