正式学习MVC 03
1、View -> Controller的数据通信
1) 通过url查询字符串
public ActionResult Index(string user)
{ return Content(user);
}
2)通过post方式传递
ViewBag.Title = "ShowForm";
} <h2>ShowForm</h2> <p>your form data will be deliver to Index</p> <form action="/Demo/Index" method="post">
<input type="text" name="user" value="" />
<input type="submit" name="" value="提交" />
</form>
public ActionResult Index(string user)
{ return Content(user);
}
public ActionResult Index(string user)
{ return Content(user);
}
public ActionResult ShowForm(string user)
{ return View();
}
如果需要指定请求方式,可通过设置属性来指定
[HttpGet]
public ActionResult Index(string user)
{ return Content(user);
}
将无法响应post请求
建议不用参数列表,直接将所需参数整合为一个类作为参数
public ActionResult Index(Models.Student model)
{ // code }
推荐这么写的原因还有可以设置Model属性
需要引用:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations; namespace MVCStudy.Models
{
public class Student
{
[Required]
public int Id { get; set; }
[Required,StringLength(maximumLength:,MinimumLength = )]
public string Name { get; set; } }
}
并在controller进行校验
public ActionResult Index(Models.Student model)
{
if (!ModelState.IsValid)
{
return Content("your data is not right");
}
return Content(model.Name);
}
如果想在视图页面进行提示。可以右键Controller内的相应方法新建视图:

设置好模型类,将会自动生成前端校验的视图(强类型视图)
此时分别编写get和post请求的controller:
[HttpGet]
public ActionResult Index()
{
return View();
}
// GET: Demo
[HttpPost]
public ActionResult Index(Models.Student model)
{
if (!ModelState.IsValid)
{
return Content("your data is not right");
}
return Content(model.Name);
}
2、讲解上述自动生成的视图页面
1)BeginForm
类似form标签,默认的Action为当前页面
@model MVCStudy.Models.Animal
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Animal</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Sex, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Sex, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Sex, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
①
BeginForm方法参数
actionName 方法名
controllerName 上述action对应的控制器名
routeValues 路由值
FormMethod 提交时请求方式
htmlAttribute html标签内的属性
Label EditorFor
LabelFor 文字的修改,在Model内可以修改
设置数据的显示方式(DataType)
public class Animal
{
[Required]
[DataType(DataType.Password)]
[Display(Name = "动物名称")]
public string Name { get; set; }
[Required]
[Display(Name = "性别")]
public string Sex { get; set; }
}
[EmailAddress] 邮箱校验
自定义字段输入错误信息:[EmailAddress(ErrorMessage="XXXXX")] ()
登录一般使用create的模板
②AntiForgeryToken
防止页面被改造,表现为一个Inpuit的隐藏域,
被改造或伪造的页面无法提交数据,使用时还要在controller里写
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(Models.Animal model)
{
if (!ModelState.IsValid)
{
return Content("your data is not right");
}
return Content(model.Name);
}
③ ValidationSummary
呈现错误信息:
在Controller内设置
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(Models.Animal model)
{
if (!ModelState.IsValid)
{
ModelState.AddModelError(key:"",errorMessage:"wrong data")
}
return Content(model.Name);
}
Controller内设置AddModelError并返回视图
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(Models.Animal model)
{
if (!ModelState.IsValid)
{
ModelState.AddModelError(key: "", errorMessage: "wrong data");
}else if (model.Name == "Desk")
{
ModelState.AddModelError(key: "", errorMessage: "这是桌子不是动物");
return View(model);
}
return Content(model.Name);
}
效果如下:

上述红色文字就是ValidationSummary
多个字段错误累积显示
⑤htmlAttributes
设置标签的属性
htmlAttributes:new{}
一般表单页面都有get和post两个请求方式
get 用于显示表单页面
post 处理提交的请求,与数据库交互等等
正式学习MVC 03的更多相关文章
- 正式学习MVC 01
1.新建项目 点击创建新项目,选择ASP.NET web应用程序,对项目进行命名后点击创建. 截图如下: 取消勾选HTTPS配置 可选择空 + mvc 或直接选定MVC 2.目录结构分析 1) App ...
- 正式学习MVC 05
1.剃须刀模板razor的使用 1)混编 循环语法 @model List<MVCStudy.Models.Student> @{ ViewBag.Title = "List&q ...
- 正式学习MVC 02
1.cookie 继续讲解MVC的内置对象cookie 相对不安全 1)保存cookie public ActionResult Index() { // 设置cookie以及过期时间 Respons ...
- 正式学习MVC 06
1.Model常用属性讲解 using System; using System.ComponentModel.DataAnnotations; namespace MVCStudy2.Models ...
- 正式学习MVC 04
1.ActionResult ActionResult是一个父类, 子类包括了我们熟知的 ViewResult 返回相应的视图 ContentResult 返回字符串 RedirectResult( ...
- 白话学习MVC(十)View的呈现二
本节将接着<白话学习MVC(九)View的呈现一>来继续对ViewResult的详细执行过程进行分析! 9.ViewResult ViewResult将视图页的内容响应给客户端! 由于Vi ...
- 学习MVC之租房网站(二)-框架搭建及准备工作
在上一篇<学习MVC之租房网站(一)-项目概况>中,确定了UI+Service的“双层”架构,并据此建立了项目 接下来要编写Common类库.配置AdminWeb和FrontWeb 一.编 ...
- 白话学习MVC(八)Action的执行二
一.概述 上篇博文<白话学习MVC(七)Action的执行一>介绍了ASP.NET MVC中Action的执行的简要流程,并且对TempData的运行机制进行了详细的分析,本篇来分析上一篇 ...
- 白话学习MVC(九)View的呈现一
一.概述 本节来看一下ASP.NET MVC[View的呈现]的内容,View的呈现是在Action执行之后进行,Action的执行生成一个ActionResult,[View的呈现]的功能就是:通过 ...
随机推荐
- Matlab高级教程_第二篇:MATLAB和C#对应数据类型的讲解(多讲一点儿C#的矩阵运算)
1. MATLAB对应C#的数据类型主要在引入的父类库MWArray当中.有如下对应规则 .NET TYPE MWArrayTYPE MATLAB Type System.Double MWNumer ...
- Notes_STL_List_And_Map
//Description: 使用STL遇到的问题 //Create Date: 2019-07-08 09:19:15 //Author: channy Notes_STL_List_And_Map ...
- rework-发出你的心声
生意人虚张声势的时候会给人什么感觉?都是些僵硬的措辞.官方的腔调.虚伪的友善.法律术语等.你一定看过这些玩意儿,就好像是机器人写出来的东西,这些公司在向你发话,而不是和你对话. 这种专业主义面具让人觉 ...
- mysql自定义函数统计订单状态:GET_ORDER_STATUS()
DELIMITER $$ USE `local_hnyz`$$ DROP FUNCTION IF EXISTS `GET_ORDER_STATUS`$$ CREATE DEFINER=`root`@` ...
- certutil
计算摘要 certutil -hashfile inst.ini MD2 certutil -hashfile inst.ini MD5 certutil -hashfile inst.ini SHA ...
- [LC] 404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- TreeMap简介
在Map集合框架中,除了HashMap以外,TreeMap也是常用到的集合对象之一.与HashMap相比,TreeMap是一个能比较元素大小的Map集合,会对传入的key进行了大小排序.其中,可以使用 ...
- CAD安装错误1625:系统策略禁止这个安装,请与系统管理员联系。
在安装Autodesk CAD/3DMAX/Maya/Revit/Inventor等的时候,出现“安装错误1625:系统策略禁止这个安装,请与系统管理员联系.”,或是Error 1625,同时还会提示 ...
- JavaScript学习总结(七)Ajax和Http状态字
转自:http://segmentfault.com/a/1190000000691919 Ajax及其工作原理 AJAX 是一种与服务器交换数据无需刷新网页的技术,最早由Google公司在谷歌地图里 ...
- 设置Fiddler来抓取Android接口数据
1.下载安装fiddler,安装包可自行百度.安装完成打开fiddler 2.将Fiddler设置远程访问PC 选择Fiddler->Tools->Fiddler Option 3.选择C ...