控制器
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using mvcDemo.Models; // 引入Modelsnamespace mvcDemo.Controllers{ public class HomeController : Controller { public ActionResult Index() { int hour = DateTime.Now.Hour; ViewBag.Greeting = hour < 12 ? "Good Morning" : "Goods Afternoon"; return View(); } [HttpGet] public ActionResult RsvpForm() { return View(); } [HttpPost] public ActionResult RsvpForm(GuestResponse guestResponse) { return View("Thanks", guestResponse); } }}
models
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace mvcDemo.Models{ public class GuestResponse { public string Name { get; set; } public string Email { get; set; } public string Phone { get; set; } public bool ? WillAttend { get; set; } }}
3.视图Index
@{ Layout = null;}Index @ViewBag.Greeting World!We're going to have an exciting party.
(To do : sell it better. Add pictures or something.) @Html.ActionLink("RSVP Now","RsvpForm");
RsvpForm
@model mvcDemo.Models.GuestResponse@{ Layout = null;}RsvpForm @using (Html.BeginForm()) {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 cant come", Value = bool.FalseString }, },"Choose an option")
}
Thanks
@{ Layout = null;}Thanks Thank you ,@Model.Name!
@if(Model.WillAttend == true) { @:Its greet that you're coming! }else { @:Sorry to hear that! }
增加验证 Models
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.ComponentModel.DataAnnotations;// 增加验证引入namespace mvcDemo.Models{ public class GuestResponse { [Required(ErrorMessage = "请输入姓名")] public string Name { get; set; } [Required(ErrorMessage = "请输入邮箱")] [RegularExpression(".+\\@.+\\..+",ErrorMessage = "请输入正确的邮箱")] public string Email { get; set; } [Required(ErrorMessage = "请输入号码")] public string Phone { get; set; } [Required(ErrorMessage = "请确认是否参加")] public bool ? WillAttend { get; set; } }}
控制器
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using mvcDemo.Models; // 引入Modelsnamespace mvcDemo.Controllers{ public class HomeController : Controller { public ActionResult Index() { int hour = DateTime.Now.Hour; ViewBag.Greeting = hour < 12 ? "Good Morning" : "Goods Afternoon"; return View(); } [HttpGet] public ActionResult RsvpForm() { return View(); } [HttpPost] public ActionResult RsvpForm(GuestResponse guestResponse) { if (ModelState.IsValid) { return View("Thanks", guestResponse); } else { return View(); } } public ActionResult About() { return View(); } public ActionResult Contact() { return View(); } }}
视图层
@model mvcDemo.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 cant come", Value = bool.FalseString }, },"Choose an option")
}
增加样式Content/Styles.css
.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;}
引入样式
@model mvcDemo.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 cant come", Value = bool.FalseString }, },"Choose an option")
}
增加bootstrap特效
@{ Layout = null;}Index We're going to have an exciting party!
And you are invited
@Html.ActionLink("RSVP Now", "RsvpForm")
@model mvcDemo.Models.GuestResponse@{ Layout = null;}RsvpForm RSVP
@using (Html.BeginForm()) { @Html.ValidationSummary()@Html.TextBoxFor(x => x.Name, new { @class = "form-control" })@Html.TextBoxFor(x => x.Email, new { @class = "form-control" })@Html.TextBoxFor(x => x.Phone, new { @class = "form-control" })@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", new { @class = "form-control" })}
增加邮件发送提醒
@{ Layout = null;}Thanks @{ try { WebMail.SmtpServer = "smtp.aliyun.com"; WebMail.SmtpPort = 25; WebMail.EnableSsl = false; WebMail.UserName = "diandodo@aliyun.com"; WebMail.Password = "xxxxxxxx"; WebMail.From = "diandodo@aliyun.com"; WebMail.Send("jiqing9006@126.com","RSVP Notification",Model.Name +" is "+ ((Model.WillAttend??false)?"":"not") +"attending" ); } catch(Exception) { @: Sorry - Can't send Email } }Thank you ,@Model.Name!
@if (Model.WillAttend == true) { @:Its greet that you're coming! } else { @:Sorry to hear that! }