博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Pro Aspnet MVC 4读书笔记(1) - Your First MVC Application
阅读量:6836 次
发布时间:2019-06-26

本文共 10087 字,大约阅读时间需要 33 分钟。

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();        }    }}
View Code

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";        }    }}
View Code

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();        }    }}
View Code

Listing 2-4. Adding to the View HTML

@{    Layout = null;}    
Index
Hello World (from the view)
View Code

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 < 12 ? "Good Morning" : "Good Afternoon";            return View();        }    }}
View Code

Listing 2-6. Retrieving a ViewBag Data Value

@{    Layout = null;}    
Index
@ViewBag.Greeting World (from the view)
View Code

Listing 2-7. Displaying Details of the Party

@{    Layout = null;}    
Index
@ViewBag.Greeting World (from the view)

We're going to have an exciting party.

(To do: sell it better. Add pictures or something.)

View Code

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; }    }}
View Code

Listing 2-9. Adding a Link to the RSVP Form

@{    Layout = null;}    
Index
@ViewBag.Greeting World (from the view)

We're going to have an exciting party.

(To do: sell it better. Add pictures or something.)

@Html.ActionLink("RSVP Now", "RsvpForm")
View Code

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 < 12 ? "Good Morning" : "Good Afternoon";            return View();        }        public ViewResult RsvpForm()        {            return View();        }    }}
View Code

Listing 2-12. The initial contents of the RsvpForm.cshtml file

@model PartyInvites.Models.GuestResponse@{    Layout = null;}    
RsvpForm
View Code

Listing 2-13. Creating a Form View

@model PartyInvites.Models.GuestResponse@{    Layout = null;}    
RsvpForm @using (Html.BeginForm()) {

Your name: @Html.TextBoxFor(x => x.Name)

Your name: @Html.TextBoxFor(x => x.Email)

Your name: @Html.TextBoxFor(x => x.Phone)

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")

}
View Code

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 < 12 ? "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);        }    }}
View Code

Listing 2-15. The Thanks View

@model PartyInvites.Models.GuestResponse@{    Layout = null;}    
Thanks

Thanks you, @Model.Name!

@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. }
View Code

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; }    }}
View Code

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 < 12 ? "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();            }        }    }}
View Code

Listing 2-18. Using the Html.ValidationSummary Help Method

@model PartyInvites.Models.GuestResponse@{    Layout = null;}    
RsvpForm @using (Html.BeginForm()) { @Html.ValidationSummary()

Your name: @Html.TextBoxFor(x => x.Name)

Your email: @Html.TextBoxFor(x => x.Email)

Your phone: @Html.TextBoxFor(x => x.Phone)

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")

}
View Code

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;}
View Code

Listing 2-20. Adding the link element to the RsvpForm view

@model PartyInvites.Models.GuestResponse@{    Layout = null;}    
RsvpForm @using (Html.BeginForm()) { @Html.ValidationSummary()

Your name: @Html.TextBoxFor(x => x.Name)

Your email: @Html.TextBoxFor(x => x.Email)

Your phone: @Html.TextBoxFor(x => x.Phone)

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")

}
View Code

Listing 2-21. Using the WebMail Helper

@model PartyInvites.Models.GuestResponse@{    Layout = null;}    
Thanks @{ 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) { @:Sorry - we coundn't send the email to confirm your RSVP. } }

Thanks you, @Model.Name!

@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. }
View Code

 

 

 

 

 

转载于:https://www.cnblogs.com/thlzhf/p/3526928.html

你可能感兴趣的文章
联想的amd电脑,Debian8.8开机后亮度值始终最大,尝试过各种方法,始终无法解决,最后debian8.8在安装开源驱动后,成功调节...
查看>>
debian8修改kde桌面语言
查看>>
PHP对于数据库的基本操作——更新数据
查看>>
How HashMap works in Java
查看>>
洛谷P2057 善意的投票
查看>>
UVa11401 Triangle Counting
查看>>
MongoDB
查看>>
深入Android 【三】 —— 组件入门
查看>>
Matlab DIP(瓦)ch11表示与描述练习
查看>>
【Echo】实验 -- 实现 C/C++下TCP, 服务器/客户端 通讯
查看>>
16、SpringBoot-CRUD错误处理机制(3)
查看>>
7、NIO--字符集Charset
查看>>
2-JSF html标签
查看>>
队列queue 代码
查看>>
Python-mysql 权限 pymysql 注入共计
查看>>
HashSet、LinkedHashSet、TreeSet
查看>>
ios 远程推送
查看>>
halcon算子翻译——compose5
查看>>
安装office2010提示要安装MSXML6.10.1129.0解决方法
查看>>
作业6随笔
查看>>