Pro Aspnet MVC 4读书笔记(1) - Your First MVC Application
Listing 2-1. The default contents of the HomeController class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace PartyInvites.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
}
Listing 2-2. Modifying the HomeController Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace PartyInvites.Controllers
{
public class HomeController : Controller
{
public string Index()
{
return "Hello World";
}
}
}
Listing 2-3. Modifying the Controller to Render a View
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace PartyInvites.Controllers
{
public class HomeController : Controller
{
public ViewResult Index()
{
return View();
}
}
}
Listing 2-4. Adding to the View HTML
@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
Hello World (from the view)
</div>
</body>
</html>
Listing 2-5. Setting Some View Data
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace PartyInvites.Controllers
{
public class HomeController : Controller
{
public ViewResult Index()
{
int hour = DateTime.Now.Hour;
ViewBag.Greeting = hour < ? "Good Morning" : "Good Afternoon";
return View();
}
}
}
Listing 2-6. Retrieving a ViewBag Data Value
@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@ViewBag.Greeting World (from the view)
</div>
</body>
</html>
Listing 2-7. Displaying Details of the Party
@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@ViewBag.Greeting World (from the view)
<p>
We're going to have an exciting party.<br />
(To do: sell it better. Add pictures or something.)
</p>
</div>
</body>
</html>
Listing 2-8. The GuestResponse Domain Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace PartyInvites.Models
{
public class GuestResponse
{
public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public bool? WillAttend { get; set; }
}
}
Listing 2-9. Adding a Link to the RSVP Form
@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@ViewBag.Greeting World (from the view)
<p>
We're going to have an exciting party.<br />
(To do: sell it better. Add pictures or something.)
</p>
@Html.ActionLink("RSVP Now", "RsvpForm")
</div>
</body>
</html>
Listing 2-10. Adding a New Action Method to the Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace PartyInvites.Controllers
{
public class HomeController : Controller
{
public ViewResult Index()
{
int hour = DateTime.Now.Hour;
ViewBag.Greeting = hour < ? "Good Morning" : "Good Afternoon";
return View();
} public ViewResult RsvpForm()
{
return View();
}
}
}
Listing 2-12. The initial contents of the RsvpForm.cshtml file
@model PartyInvites.Models.GuestResponse @{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>RsvpForm</title>
</head>
<body>
<div> </div>
</body>
</html>
Listing 2-13. Creating a Form View
@model PartyInvites.Models.GuestResponse @{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>RsvpForm</title>
</head>
<body>
@using (Html.BeginForm()) {
<p>Your name: @Html.TextBoxFor(x => x.Name)</p>
<p>Your name: @Html.TextBoxFor(x => x.Email)</p>
<p>Your name: @Html.TextBoxFor(x => x.Phone)</p>
<p>
Will you attend?
@Html.DropDownListFor(x => x.WillAttend,
new[]{
new SelectListItem(){Text = "Yes, I'll be there", Value = bool.TrueString},
new SelectListItem(){Text = "No, I can't come", Value = bool.FalseString}
},
"Choose an option")
</p>
<input type="submit" value="Submit RSVP" />
}
</body>
</html>
Listing 2-14. Adding an Action Method to Support POST Requests
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PartyInvites.Models; namespace PartyInvites.Controllers
{
public class HomeController : Controller
{
public ViewResult Index()
{
int hour = DateTime.Now.Hour;
ViewBag.Greeting = hour < ? "Good Morning" : "Good Afternoon";
return View();
} [HttpGet]
public ViewResult RsvpForm()
{
return View();
} [HttpPost]
public ViewResult RsvpForm(GuestResponse guestResponse)
{
// TODO: Email response to the party organizer
return View("Thanks", guestResponse);
}
}
}
Listing 2-15. The Thanks View
@model PartyInvites.Models.GuestResponse @{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Thanks</title>
</head>
<body>
<div>
<h1>Thanks you, @Model.Name!</h1>
@if (Model.WillAttend == true) {
@:It's great that you're coming. The drinks area already in the fridge!
} else {
@:Sorry to hear that you can't make it, but thanks for letting us know.
}
</div>
</body>
</html>
Listing 2-16. Applying Validation to the GuestResponse Model Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations; namespace PartyInvites.Models
{
public class GuestResponse
{
[Required(ErrorMessage = "Please enter your name")]
public string Name { get; set; } [Required(ErrorMessage = "Please enter your email address")]
[RegularExpression(".+\\@.+\\..+",
ErrorMessage = "Please enter a valid email address")]
public string Email { get; set; } [Required(ErrorMessage = "Please enter your phone number")]
public string Phone { get; set; } [Required(ErrorMessage = "Please specify whether you'll attend")]
public bool? WillAttend { get; set; }
}
}
Listing 2-17. Checking for Form Validation Errors
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PartyInvites.Models; namespace PartyInvites.Controllers
{
public class HomeController : Controller
{
public ViewResult Index()
{
int hour = DateTime.Now.Hour;
ViewBag.Greeting = hour < ? "Good Morning" : "Good Afternoon";
return View();
} [HttpGet]
public ViewResult RsvpForm()
{
return View();
} [HttpPost]
public ViewResult RsvpForm(GuestResponse guestResponse)
{
if (ModelState.IsValid)
{
// TODO: Email response to the party organizer
return View("Thanks", guestResponse);
}
else
{
// there is validation error
return View();
}
}
}
}
Listing 2-18. Using the Html.ValidationSummary Help Method
@model PartyInvites.Models.GuestResponse @{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>RsvpForm</title>
</head>
<body>
@using (Html.BeginForm()) {
@Html.ValidationSummary()
<p>Your name: @Html.TextBoxFor(x => x.Name)</p>
<p>Your email: @Html.TextBoxFor(x => x.Email)</p>
<p>Your phone: @Html.TextBoxFor(x => x.Phone)</p>
<p>
Will you attend?
@Html.DropDownListFor(x => x.WillAttend,
new[]{
new SelectListItem(){Text = "Yes, I'll be there", Value = bool.TrueString},
new SelectListItem(){Text = "No, I can't come", Value = bool.FalseString}
},
"Choose an option")
</p>
<input type="submit" value="Submit RSVP" />
}
</body>
</html>
Listing 2-19. The contents of the Content/Site.css file
.field-validation-error {
color: #f00;
} .field-validation-valid {
display: none;
} .input-validation-error {
border: 1px solid #f00;
background-color: #fee;
} .validation-summary-errors {
font-weight: bold;
color: #f00;
} .validation-summary-valid {
display: none;
}
Listing 2-20. Adding the link element to the RsvpForm view
@model PartyInvites.Models.GuestResponse @{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<link rel="stylesheet" type="text/css" href="~/Content/Site.css" />
<title>RsvpForm</title>
</head>
<body>
@using (Html.BeginForm()) {
@Html.ValidationSummary()
<p>Your name: @Html.TextBoxFor(x => x.Name)</p>
<p>Your email: @Html.TextBoxFor(x => x.Email)</p>
<p>Your phone: @Html.TextBoxFor(x => x.Phone)</p>
<p>
Will you attend?
@Html.DropDownListFor(x => x.WillAttend,
new[]{
new SelectListItem(){Text = "Yes, I'll be there", Value = bool.TrueString},
new SelectListItem(){Text = "No, I can't come", Value = bool.FalseString}
},
"Choose an option")
</p>
<input type="submit" value="Submit RSVP" />
}
</body>
</html>
Listing 2-21. Using the WebMail Helper
@model PartyInvites.Models.GuestResponse @{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Thanks</title>
</head>
<body>
@{
try
{
WebMail.SmtpServer = "smtp.example.com";
WebMail.SmtpPort = 587;
WebMail.EnableSsl = true;
WebMail.UserName = "mySmtpUsername";
WebMail.Password = "mySmtpPassword";
WebMail.From = "rsvps@example.com"; WebMail.Send("party-host@example.com", "RSVP Notification",
Model.Name + " is " + ((Model.WillAttend ?? false) ? " " : "not ") + "attending");
}
catch(Exception)
{
@:<b>Sorry - we coundn't send the email to confirm your RSVP.</b>
}
}
<div>
<h1>Thanks you, @Model.Name!</h1>
@if (Model.WillAttend == true) {
@:It's great that you're coming. The drinks area already in the fridge!
} else {
@:Sorry to hear that you can't make it, but thanks for letting us know.
}
</div>
</body>
</html>
Pro Aspnet MVC 4读书笔记(1) - Your First MVC Application的更多相关文章
- 【Tools】Pro Git 一二章读书笔记
记得知乎以前有个问题说:如果用一天的时间学习一门技能,选什么好?里面有个说学会Git是个很不错选择,今天就抽时间感受下Git的魅力吧. Pro Git (Scott Chacon) 读书笔记: ...
- [Git00] Pro Git 一二章读书笔记
记得知乎以前有个问题说:如果用一天的时间学习一门技能,选什么好?里面有个说学会Git是个很不错选择,今天就抽时间感受下Git的魅力吧. Pro Git (Scott Chacon) 读书笔记: ...
- Pro Aspnet MVC 4读书笔记(4) - Working with Razor
Listing 5-1. Creating a Simple Domain Model Class using System; using System.Collections.Generic; us ...
- Pro Aspnet MVC 4读书笔记(3) - Essential Language Features
Listing 4-1. The Initial Content of the Home Controller using System; using System.Collections.Gener ...
- Pro Aspnet MVC 4读书笔记(2) - The MVC Pattern
Listing 3-1. The C# Auction Domain Model using System; using System.Collections.Generic; using Syste ...
- Pro Aspnet MVC 4读书笔记(5) - Essential Tools for MVC
Listing 6-1. The Product Model Class using System; using System.Collections.Generic; using System.Li ...
- 【读书笔记】Asp.Net MVC 上传图片到数据库(会的绕行)
之前上传图片的做法都是上传到服务器上的文件夹中,再将url保存到数据库.其实在MVC中将图片上传到数据库很便捷的事情,而且不用去存url了.而且这种方式支持ie6(ie6不支持jquery自动提交fo ...
- (读书笔记)Asp.net Mvc 与WebForm 混合开发
根据项目实际需求,有时候会想在项目中实现Asp.net Mvc与Webform 混合开发,比如前台框架用MVC,后台框架用WebForm.其实要是实现也很简单,如下: (1)在MVC 中使用Webfo ...
- 读书笔记-常用设计模式之MVC
1.MVC(Model-View-Controller,模型-视图-控制器)模式是相当古老的设计模式之一,它最早出现在SmallTalk语言中.MVC模式是一种复合设计模式,由“观察者”(Observ ...
随机推荐
- 漫游Kafka介绍章节简介
原文地址:http://blog.csdn.net/honglei915/article/details/37564521 介绍 Kafka是一个分布式的.可分区的.可复制的消息系统.它提供了普通消息 ...
- Mono+CentOS+Jexus
在.NET Core之前,实现.Net跨平台之Mono+CentOS+Jexus初体验准备工作 本篇文章采用Mono+CentOS+Jexus的方式实现部署.Net的Web应用程序(实战,上线项目). ...
- 性能是全新的 SEO
作为一个前端project师,那不只就是公开地处理那些美丽的html5, css3 和javascript特效.小而重要的一部分工作就是要让项目朝着代码稳定和代码标准方向进展.设计.信息结构以及后台限 ...
- 【转】QT样式表 (QStyleSheet)
作者:刘旭晖 Raymond 转载请注明出处Email:colorant@163.comBLOG:http://blog.csdn.net/colorant/ 除了子类化Style类,使用QT样式表( ...
- 使用CMakeLists.txt 判断编译器是否支持C++11
#将下面的内容添加到CMakeLists.txt当中include(CheckCXXCompilerFlag) CHECK_CXX_COMPILER_FLAG("-std=c++11&quo ...
- eclipse-jee 配置tomcat7,解决404错误
在eclipse的Servers窗口新建一个tomcat7,配置tomcat的安装路径,然后启动tomcat,访问http://localhost:8080/,但是报404错误,恼火!没有找到要访问的 ...
- strategy pattern
- git commit -s -m 注释中的换行 [加入signed-off-by
windows环境下的Git Bash中注释的换行: 使用单引号. 或者是在Linux系统里面用终端 git add . git commit -m ' . this is the test . up ...
- base 64 编解码器
base 64 编解码 1. base64的编码都是按字符串长度,以每3个8bit的字符为一组, 2. 然后针对每组.首先获取每一个字符的ASCII编码. 3. 然后将ASCII编码转换成8bit的二 ...
- C++使用对象指针
//定义结构 Box.h: #ifndef BOX_H #define BOX_H struct Box{ double length; double width; double height; do ...