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

Style.css

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web; 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; }
}
}

Model__GuestResponse.cs

在view文件夹中,Home控制器内存在3个主视图

@{
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.<br />(To do: sell it better. Add pictures or something.)</h2>
<h3>And you are invited</h3>
<div class="btn btn-success">
@Html.ActionLink("RSVP Now", "RsvpForm")
</div>
</div>
</body>
</html>

View→Home→Index.cshtml

@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.min.css" rel="stylesheet" />
<link href="~/Content/style.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">
<label>Your name:</label> @Html.TextBoxFor(x => x.Name, new { @class = "form-control" })
</div>
<div class="form-group">
<label>Your email:</label> @Html.TextBoxFor(x => x.Email, new { @class = "form-control" })
</div>
<div class="form-group">
<label>Your phone:</label> @Html.TextBoxFor(x => x.Phone, new { @class = "form-control" })
</div>
<div class="form-group">
<label>Will you attend?</label>
@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" })
</div>
<div class="text-center"><input class="btn btn-success" type="submit" name="name" value="Submit RSVP" /></div>
}
</div>
</div>
</body>
</html>

View→Home→RsvpForm.cshtml

@model PartyInvites.Models.GuestResponse

@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Thanks</title>
</head>
<body>
<div>
<h1>Thank you,@Model.Name!</h1>
@if (@Model.WillAttend == true) {
@:It's great that you're coming.The drinks are already in the fridge.
}
else {
@:Sory to hear that you can't make it,but thanks for letting us know.
}
</div>
</body>
</html>

View→Home→Thanks.cshtml

controller

using PartyInvites.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace PartyInvites.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
//public ActionResult Index()
//{
// return View();
//}
public ViewResult Index() {
int hour = DateTime.Now.Hour;
ViewBag.Greeting = hour < ? "Good Morning" : "Good Afternoon";
return View();
}
public ViewResult RsvpForm() {
return View();
}
[HttpPost]
public ViewResult RsvpForm(GuestResponse guestResponse)
{
if (ModelState.IsValid)
{
return View("Thanks", guestResponse);
}
else {
return View();
}
}
}
}

HomeController.cs

MVC 小demo的更多相关文章

  1. 【Java】Jsoup爬虫,一个简单获取京东商品信息的小Demo

    简单记录 - Jsoup爬虫入门实战 数据问题?数据库获取,消息队列中获取中,都可以成为数据源,爬虫! 爬取数据:(获取请求返回的页面信息,筛选出我们想要的数据就可以了!) 我们经常需要分析HTML网 ...

  2. 新手 gulp+ seajs 小demo

    首先,不说废话,它的介绍和作者就不在多说了,网上一百度一大堆: 我在这里只是来写写我这2天抽空对seajs的了解并爬过的坑,和实现的一个小demo(纯属为了实现,高手请绕道); 一.环境工具及安装 1 ...

  3. Nancy之基于Nancy.Hosting.Self的小Demo

    继昨天的Nancy之基于Nancy.Hosting.Aspnet的小Demo后, 今天来做个基于Nancy.Hosting.Self的小Demo. 关于Self Hosting Nancy,官方文档的 ...

  4. Nancy之基于Nancy.Owin的小Demo

    前面做了基于Nancy.Hosting.Aspnet和Nancy.Hosting.Self的小Demo 今天我们来做个基于Nancy.Owin的小Demo 开始之前我们来说说什么是Owin和Katan ...

  5. Nancy之基于Self Hosting的补充小Demo

    前面把Hosting Nancy with ASP.NET.Self Hosting Nancy和Hosting Nancy with OWIN 以demo的形式简单描述了一下. 这篇是为Self H ...

  6. [Unity3D]做个小Demo学习Input.touches

    [Unity3D]做个小Demo学习Input.touches 学不如做,下面用一个简单的Demo展示的Input.touches各项字段,有图有真相. 本项目已发布到Github,地址在(https ...

  7. Android -- 自定义View小Demo,动态画圆(一)

    1,转载:(http://blog.csdn.NET/lmj623565791/article/details/24500107),现在如下图的效果: 由上面的效果图可以看到其实是一个在一个圆上换不同 ...

  8. Win10 FaceAPI小demo开发问题汇总

    Win10 FaceAPI小demo开发问题汇总 最近使用微软牛津计划做一个小demo,使用FaceAPI做一个小应用,实现刷脸的功能.开发的过程中用到几个问题,具体如下: Stream 与IRand ...

  9. 模仿京东顶部搜索条效果制作的一个小demo

    最近模仿京东顶部搜索条效果制作的一个小demo,特贴到这里,今后如果有用到可以参考一下,代码如下 #define kScreenWidth [UIScreen mainScreen].bounds.s ...

随机推荐

  1. 浅析java中clone()方法

    本文转载自:http://blog.csdn.net/mengxiangyue/article/details/6818611 Java中我们可能都遇到过这样的情况,在我们将一个对象做为参数传给一个函 ...

  2. JQuery——关于CDN(内容分发网络)

    替代方案 如果您不希望下载并存放 jQuery,那么也可以通过 CDN(内容分发网络) 引用它. Staticfile CDN.百度.又拍云.新浪.谷歌和微软的服务器都存有 jQuery . 如果你的 ...

  3. Delphi 参数的传递

  4. Delphi SpeedButton组件

  5. 利用shell脚本做一个用户登录系统

    效果图如下: #!/bin/bash# while truedocat << EOF//======================\\\\| 用户登录系统 |-------------- ...

  6. Apache提示You don't have permission to access / on this server 解决

    本文链接:https://blog.csdn.net/Niu_Eva/article/details/90741894 Apache提示You don’t have permission to acc ...

  7. Ubuntu下查看so文件的函数列表

    Ubuntu下查看so文件的函数列表   可使用如下命令: 1.nm -D XXX.so 2.objdump -tT  XXX.so   nm libcyusb.so | grep "usb ...

  8. 使用TortoiseGit,设置ssh方式连接git仓库。

    开始设置之前的准备:建立项目文件夹,初始化git仓库(右键 git  init),右键打开 git bash ,git pull “仓库地址”, 把网站上的仓库代码拉取下来. TortoiseGit使 ...

  9. 第二章 Vue快速入门-- 28 自定义按键修饰符

    事件处理-按键修饰符 js 里面的键盘事件对应的键码 <!DOCTYPE html> <html lang="en"> <head> <m ...

  10. 安装theano时候发现报错:cannot install ''numpy'.It is a distutils installed project and thus we cannot ...

    发现我安装theano的时候需要numpy需要1.9以上版本,而我之前自带的numpy是1.8版本,所以版本有问题.根本原因是theano需要的numpy版本不符合要求,但是numpy已经安装过了,所 ...