“21天好习惯”第一期-7

mvc小项目练习操作

一个模拟发送邀请函的过程

运行效果如下

 

错误验证提示

 弹出结果有

 或

 

 

Home控制器代码

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
    {
        // GET: Home
        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:给晚会的组织者发送邮件
                return View("Thanks", guestResponse);
            }
            else {
                //验证有错误
                return View();
            }
           
        }
    }
}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
    {
        // GET: Home
        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:给晚会的组织者发送邮件
                return View("Thanks", guestResponse);
            }
            else {
                //验证有错误
                return View();
            }
           
        }
    }
}

类,添加了验证

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

index视图


@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <link href="~/Content/bootstrap.css" rel="stylesheet" />
    <link href="~/Content/bootstrap-theme.css" rel="stylesheet" />
    <title>Index</title>
    <style>
        .btn a {
        color:white;
        text-decoration:none;
        }
        body {
        background-color:#F1F1F1;
        }
    </style>
</head>
<body>
    <div class="text-center">
        <h2>We're going to have an exciting party!</h2>
        <h3>And you are invited.</h3>
        <!-- @*@ViewBag.Greeting World(from the view) *@-->
        <div class="btn btn-success">
            @Html.ActionLink("PSVP Now", "RsvpForm")
        </div>

    </div>
</body>
</html>

RsvpForm视图

@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/Styles.css" />
    <link href="~/Content/bootstrap.css" rel="stylesheet" />
    <link href="~/Content/bootstrap-theme.css" rel="stylesheet" />
   
    <title>RsvpForm</title>
</head>
<body>
    <div class="panel panel-success">
        <div class="panel-heading text-center"><h4>RSVP</h4></div>
        <div class="panel-body">
            @using (Html.BeginForm())
            {
                @Html.ValidationSummary()
                <div class="form-group">
                    <lable> Your name:</lable>
                    @Html.TextBoxFor(x => x.Name, new { @class = "formcontrol" })
                </div>
                <div class="form-group">
                    <lable> Your email:</lable>
                    @Html.TextBoxFor(x => x.Email, new { @class = "formcontrol" })
                </div>
                <div class="form-group">
                    <lable> Your phone:</lable>
                    @Html.TextBoxFor(x => x.Phone, new { @class = "formcontrol" })
                </div>
                <div class="form-group">
                    <lable> Will you attend?</lable>
                    @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 = "formcontrol" })
                </div>
                <div class="text-center">
                    <input class="btn btn-success" type="submit" value="Submit RSVP" />
                </div>
            }

        </div>
    </div>
    
        @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" />
        }
        <div>
        </div>
    
</body>
</html>

Thanks视图

@model PartyInvites.Models.GuestResponse

@{
    Layout = null;


}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <link href="~/Content/bootstrap.css" rel="stylesheet" />
    <link href="~/Content/bootstrap-theme.css" rel="stylesheet" />
    <title>Thanks</title>
    <style>
        body {
        background-color:#F1F1F1;
        }
    </style>
</head>
<body>
    @{  try
        {
            WebMail.SmtpServer = "smpt.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 chould't send the email to confirm your RSVP</b>
        }
    }
<div class="text-center">
    <h1>Thank you, @Model.Name ! </h1>
    <div class="lead">
        @if (Model.WillAttend == true)
        {
            @:It's grewt that you're coming the drinks are already in the fridge!
        }
        else
        {
            @:Sorry to hear that you can't make it, but thanks for letting us know.
        }
    </div>

</div>
</html>

Style样式

body {
}
.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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Redmonster0923

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值