一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第17天,点击查看活动详情。
传值:
1.ViewData["name"]="李四" --键值对 ,获取时要类型转换 单页面
- ViewBag.Accounts = accounts; --- 单页面,获取不用类型转换
3.TemData["name"] = "李四" --可以跨页面,获取了就释放
取值:
List<Account> list = ViewBag.Accounts;
@foreach(Account account in list)
{
<tr>
<td>@account.AccountId</td>
<td>@account.AccountName</td>
<td>@account.AccountPwd</td>
<td>@account.AccountSex</td>
<td>@account.AccountEmail</td>
</tr>
}
跳转:
页面中跳转链接:
<span><a href="/GameDB/Add">添加</a></span>
表单提交:
<form method="post" action="/GameDB/Save">
转到页面:
//自动跳转到Add.cshtml页面
public IActionResult Add()
{
return View();
}
//跳转到指定页面
public IActionResult Add()
{
return View("AddView");
}
//跳转页面,传递模型
return View("AddView",u);
//经过控制器,并跳转页面
return Redirect("Index");
使用URL跳转和Html跳转(不会受路由规则变化的影响):
<a href="@Url.Action("Add","GameDB",new {name="tom"})>通过内置的Url方法跳转到Add</a>
@Html.ActionLink("通过Html跳转到Add","Add","GameDB",new {name="tom"}, new {style="color:red"});
组件:
自动装配是MVC特有的技术,能够将ViewData和ViewBag传递过来的数据自动装配
@Html.TextBox("Username","请输入账号",new {style="color:red"},@class="danger");
//List<SelectListItem> List = ViewData["data"] as List<SelectListItem>
//通过ViewData的键传递过来的值,这里DropList会完成数据的自动装配
@Html.DropDownList("data")
@using (@Html BeginForm("about","home",FormMethod.Post,new {name="tom"})){
@Html.TextBox("username","")
}
@Html.RadioButton("rb1","1","true")
强类型
1、单个对象时:
方法一:
控制器中: ViewData.Model=u;
视图中:@model demo.models.User
取值:@Model.Name
方法二:
控制器中:return View("index",u);
视图中:@model demo.models.User
取值:@Model.Name
2,多个对象时:
控制器中: ViewData.Model=db.Users.ToList();
视图中:@List<model demo.models.User>
取值:遍历
上传文件:
前端视图:
1.需要一个表单进行数据的提交,表单应该添加一个熟悉:enctype="multipart/form-data"
2.需要表单添加一个用于上传文件的标签(file),需要提交按钮
后端的控制器:
用HttpPostedFileBse myfile接收:
文件上传:
if(myfile!=null)
{
myfile.SaveAs(Server.MapPath("~/images/"+myfile.FileName ))
}
u.photo=myfile.FileName
模型注解验证
数据验证API位于System.ComponentModel.DataAnnotations命名空间
Compar: 示例: [Compare ("password")]
Range: 示例: [Range(10,20)]
RegularExpression: 示例: [RegularExpression("pattern")]
Required : 示例: [Required ]
StringLength: 示例: [StringLength(10)]
Display : 示例: [Display(Name="用户“)]
在视图中通过强类型进行绑定以后
控制器的方法:
public ActionResult Show(UserInfo u){
if(ModelState.IsValid){
return View("show2",u)
}else
{
return View("index")
}
}
使用Ajax.ActionLink
1.安装Jquery.Unobtrusive.Ajax包
2.在视图:
@Ajax.ActionLink("获取时间","getdate","home",new AjaxOption{
Confirm="确认要提交吗",
HttpMethod="post",
InsertionMode=InsertionMode.Replace,
OnSuccess="msg",
UpdateTargetId="show"
})
<div id="show"></div>
使用Ajax.BeginForm
//视图
@using(Ajax.BeginForm("UserRegister","home",null,new AjaxOption{
Confirm="确认要提交吗",
HttpMethod="post",
InsertionMode=InsertionMode.Replace,
UpdateTargetId="show"
})))
<table>
<tr>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td><input type="password" name="password"></td>
</tr>
</table>
<div id="show"></div>
// homeController.cs
publci IActionResult UserRegister(string username,string password){
ViewBag.username=username;
ViewBag.password=password;
//这里将result视图返回给show的div里
//return View("result")
return PartialView("result") //局部视图
}
项目部署
1、控制面板=》查看方式为大小图标=》选择程序和功能=》启用或关闭windows功能(或者直接搜索启用环关闭windows功能)
2、勾选IIS管理控制台,Asp.net4.7以上版本
3、VS项目右键发布:
服务器:localhost
站点名称:Default Web Site/Game