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的更多相关文章

  1. 正式学习MVC 01

    1.新建项目 点击创建新项目,选择ASP.NET web应用程序,对项目进行命名后点击创建. 截图如下: 取消勾选HTTPS配置 可选择空 + mvc 或直接选定MVC 2.目录结构分析 1) App ...

  2. 正式学习MVC 05

    1.剃须刀模板razor的使用 1)混编 循环语法 @model List<MVCStudy.Models.Student> @{ ViewBag.Title = "List&q ...

  3. 正式学习MVC 02

    1.cookie 继续讲解MVC的内置对象cookie 相对不安全 1)保存cookie public ActionResult Index() { // 设置cookie以及过期时间 Respons ...

  4. 正式学习MVC 06

    1.Model常用属性讲解 using System; using System.ComponentModel.DataAnnotations; namespace MVCStudy2.Models ...

  5. 正式学习MVC 04

    1.ActionResult ActionResult是一个父类, 子类包括了我们熟知的 ViewResult 返回相应的视图 ContentResult  返回字符串 RedirectResult( ...

  6. 白话学习MVC(十)View的呈现二

    本节将接着<白话学习MVC(九)View的呈现一>来继续对ViewResult的详细执行过程进行分析! 9.ViewResult ViewResult将视图页的内容响应给客户端! 由于Vi ...

  7. 学习MVC之租房网站(二)-框架搭建及准备工作

    在上一篇<学习MVC之租房网站(一)-项目概况>中,确定了UI+Service的“双层”架构,并据此建立了项目 接下来要编写Common类库.配置AdminWeb和FrontWeb 一.编 ...

  8. 白话学习MVC(八)Action的执行二

    一.概述 上篇博文<白话学习MVC(七)Action的执行一>介绍了ASP.NET MVC中Action的执行的简要流程,并且对TempData的运行机制进行了详细的分析,本篇来分析上一篇 ...

  9. 白话学习MVC(九)View的呈现一

    一.概述 本节来看一下ASP.NET MVC[View的呈现]的内容,View的呈现是在Action执行之后进行,Action的执行生成一个ActionResult,[View的呈现]的功能就是:通过 ...

随机推荐

  1. ubuntu或者raspbian清理软件使用痕迹

    拿最常用的nginx举例 删除nginx–purge包括配置文件 sudo apt-get --purge remove nginx 开始使用上面这条,后来发现还是有很多相关联没有删除 首先需要停止n ...

  2. [LC] 299. Bulls and Cows

    Example 1: Input: secret = "1807", guess = "7810" Output: "1A3B" Expla ...

  3. 吴裕雄--天生自然 JAVA开发学习:MySQL 连接

    CREATE TABLE `websites` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL DEFAULT '' ...

  4. MyEclipse10安装properties文件插件

    安装步骤 1.下载PropertiesEditor插件 http://propedit.sourceforge.jp/index_en.html 2.解压出features.plugins文件 3.在 ...

  5. Hypothesis Tests for One Population Mean When σ Is Unknown|other

    9.5 Hypothesis Tests for One Population Mean When σ Is Unknown 使用t分布: What If the Assumptions Are No ...

  6. 微软要冷落windows是天方夜谭还是势在必行

    自从90年代开始,微软就统治着PC桌面市场,真正意义上地改变了世界,在很长一段时间内,Windows就和阳光.空气.水.电能一样,成为生活必需品,无处不在,又让人感觉不到其存在,正因如此,微软的市值于 ...

  7. springboot学习笔记:7.IDEA下5步完成热部署配置

    开发工具IDEA 2017.02   JDK1.8 1.pom.xml中增加: <dependency> <groupId>org.springframework.boot&l ...

  8. android高仿抖音、点餐界面、天气项目、自定义view指示、爬取美女图片等源码

    Android精选源码 一个爬取美女图片的app Android高仿抖音 android一个可以上拉下滑的Ui效果 android用shape方式实现样式源码 一款Android上的新浪微博第三方轻量 ...

  9. Fault Domain深入分析

  10. Netflix拒上戛纳电影节,能给国内视频产业什么启示?

    当新事物诞生时,总是会遭到质疑,甚至是排斥!因为新事物的活力.潜力,都对保守的传统事物产生了极大的冲击.就像有声电影刚刚诞生时,一代"默片大师"卓别林就对其进行了激烈的反对.他认为 ...