.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. Intel Coleto Creek SSL chipset

    Intel Coleto Creek SSL chipset name type interface speed model SR-IOV driver Intel SSL chipset Colet ...

  2. Git复习(六)之标签管理

    标签管理 发布一个版本时,我们通常先在版本库中打一个标签(tag),这样,就唯一确定了打标签时刻的版本.将来无论什么时候,取某个标签的版本,就是把那个打标签的时刻的历史版本取出来.所以,标签也是版本库 ...

  3. vscode中外部引入js文件以及里面相应的方法

    随便写一个js文件,定义一个方法名,并且在组件中进行引用就算成功 ###~js function shuchu(){     console.log(1) } export { shuchu };   ...

  4. 第十二篇 JavaScript(简称JS) 实现显示与隐藏

    JavaScript   JavaScript简称JS.JS是脚本语言,它是一种轻量级的编程语言,是可以插入HTML页面的编程代码,几乎所有现代浏览器都是支持的. 理论老师不行,我就抄袭手册上的一些关 ...

  5. appium基础之简单的小例子

    appium环境搭建了,当然也要开始用起来了,记录一下学习的过程 遇到问题 1.The permission to start '.ui.home.view.HomeActivity' activit ...

  6. ORACLE (BLOB、CLOB、NCLOB、BFILE)

    LOB类型 内置的LOB数据类型包括BLOB.CLOB.NCLOB.BFILE(外部存储)的大型化和非结构化数据,如文本.图像.视屏.空间数据存储.BLOB.CLOB.NCLOB类型 4.1 CLOB ...

  7. zabbix 邮件报警事件:Zabbix discoverer processes more than 75% busy

    Problem has been resolved at :: on Problem name: Zabbix discoverer processes more than % busy Host: ...

  8. cmake编译c++程序

    当在Linux系统下编写程序时候,如果没有类似于visual studio.vs code等IDE(集成开发环境)时,如何编译.运行程序呢?一种方法是编写makefile文件,用makefile文件管 ...

  9. 2017 ICPC 乌鲁木齐

    H:题目看错 背锅 #include <bits/stdc++.h> #include <vector> #define PI acos(-1.0) #define mem(a ...

  10. STARTUPINFO结构体

    typedef struct _STARTUPINFO { DWORD cb; //包含STARTUPINFO结构中的字节数.如果Microsoft将来扩展该结构,它可用作版本控制手段.应用程序必须将 ...