《精通ASP.NET MVC5》第2章 第一个MVC应用程序

控制器
public class NewHomeController : Controller
{
// GET: /NewHome/
public ActionResult Index()
{
int hour = DateTime.Now.Hour;
ViewBag.Greeting = hour < 12 ? "早上好!" : "下午好!";
return View();
}
[HttpGet]
public ViewResult RsvpForm()
{
return View();
}
[HttpPost]
public ViewResult RsvpForm(GuestResponse guestResponse)
{
if (ModelState.IsValid)
{
//发送Email
return View("Thanks", guestResponse);
}
else
{
return View();
}
}
}
视图 Index.cshtml
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@ViewBag.Greeting World(from the view)
<p>我们将举办一场聚会。</p>
@Html.ActionLink("RSVP Now","RsvpForm")
</div>
</body>
</html>
视图 RsvpForm.cshtml
@model EF6.Models.GuestResponse
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>RsvpForm</title>
<link rel="stylesheet" type="text/css" href="@Url.Content("~/CSS/Html.error.css")"/>
</head>
<body>
@using (Html.BeginForm())
{
@Html.ValidationMessageFor(x=>x.Name)
<p>Your name: @Html.TextBoxFor(x => x.Name) </p>
@Html.ValidationMessageFor(x => x.Email)
<p>Your email: @Html.TextBoxFor(x => x.Email)</p>
@Html.ValidationMessageFor(x => x.Phone)
<p>Your phone: @Html.TextBoxFor(x => x.Phone)</p>
@Html.ValidationMessageFor(x => x.WillAttend)
<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" />
}
</body>
</html>
视图 Thanks.cshtml
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Thanks</title>
</head>
<body>
<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
{
@:Sorry to hear that you can't make it, but thanks for letting us know.
}
</body>
</html>
样式 CSS/Html.error.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}
.field-validation-valid{display: none}
《精通ASP.NET MVC5》第2章 第一个MVC应用程序的更多相关文章
- 第六章第一个linux个程序:统计单词个数
第六章第一个linux个程序:统计单词个数 从本章就开始激动人心的时刻——实战,去慢慢揭开linux神秘的面纱.本章的实例是统计一片文章或者一段文字中的单词个数. 第 1 步:建立 Linu x 驱 ...
- 第一章 第一个spring boot程序(转载)
第一章 第一个spring boot程序 本编博客转发自:http://www.cnblogs.com/java-zhao/p/5324185.html 环境: jdk:1.8.0_73 mave ...
- 第一个 MVC 应用程序(上半部分)(《精通 ASP.NET MVC5》 的第二章)
本章将使用 ASP.NET MVC 框架创建一个简单的数据录入应用程序. 笔者会将过程分解成一个个的步骤,以便能够看出如何构造 ASP.NET MVC 应用程序.(对于一些未进行解释的内容,笔者提供了 ...
- ASP.NET MVC5 第4章
参考资料<ASP.NET MVC5 高级编程>第5版 第4章 模型 本章所探讨的模型是要显示.保存.创建.更新和删除的对象. 基架指使用 MVC 提供的工具为每个模型对象的标准索引构建.创 ...
- Pro ASP.NET MVC –第二章 第一个MVC程序
学习一个软件开发框架的最有效的方式就是了解并使用它.在本章,你将会创建一个简单基于ASP.NET MVC Framework的数据-实体应用程序.我们会该程序划分成若干小块,每次介绍一个部分,以便你能 ...
- ASP.NET + MVC5 入门完整教程八 -—-- 一个完整的应用程序(上)
https://blog.csdn.net/qq_21419015/article/details/80509513 SportsStore 1.开始创建Visual Studio 解决方案和项目这里 ...
- ASP.NET + MVC5 入门完整教程三 (下) ---MVC 松耦合
建立松耦合组件 MVC 模式最重要的特性之一视他支持关注分离,希望应用程序中的组件尽可能独立,只有很少的几个可控依赖项.在理想的情况下,每个组件都不了解其他组件,而只是通过抽象接口来处理应用程序的其他 ...
- ASP.NET MVC3入门教程之第一个WEB应用程序
本文转载自:http://www.youarebug.com/forum.php?mod=viewthread&tid=91&extra=page%3D1 上一节,我们已经搭建好了AS ...
- 《精通ASP.NET MVC5》第7章 SportStore:一个真正的应用程序(1)
7.1 开始 7.1.1 解决方案 个工程. 1. 一个域模块工程. 2.一个MVC4应用. 3.一个单元测试工程. 现在我们就创建一个名为 SportsStore 的空 soluti ...
随机推荐
- UVa 540 Team Queue 【STL】
题意:给出t个团体,这t个团体排在一起,每次新来一个x排队,如果在整个的团体队列中,有x的队友,那么x排在它的队友的后面,如果他没有队友,则排在长队的队尾 求给出的每一个出队命令,输出出队的人的编号 ...
- main cannot be resolved or is not a field
今天在做XML解析的时候,总是给我报 XML Parsing Error: XML or text declaration not at start of entity 的错误,后来查了下讲大概意思是 ...
- codevs 1135 选择客栈
这题没什么话说. #include<iostream> #include<cstdio> #include<cstring> #include<algorit ...
- 从投影的角度理解pca:向量,投影,基,内积,坐标,维数,分散程度,方差,协方差矩阵,对角化,特征值分解,主成分分析PCA
参考:http://blog.csdn.net/songzitea/article/details/18219237
- noip2006提高组题解
第一题:能量项链 区间型动态规划 据说这题在当年坑了很多人. f(i, j) 表示从第i个珠子开始合并j个珠子所释放的最大能量. f(i, j) = max{ f(i, k} + f(i+k, j-k ...
- ActionBarSherlock的学习笔记(四) ------------ ActionBarSherlock中的搜索及SearchView的使用
在使用ActionBarSherlock定义app的头部操作时,会经常看见搜索的动作,本文主要介绍一下搜索是如何实现的. 1. SearchView 是搜索的核心组件,具体介绍请参考Android官方 ...
- 转载RabbitMQ入门(3)--发布和订阅
发布和订阅 (使用java 客户端) 在先前的指南中,我们创建了一个工作队列.这工作队列后面的假想是每一个任务都被准确的传递给工作者.在这部分我们将会做一些完全不同的事情–我们将一个消息传递给多个消费 ...
- System.arraycopy方法
数组的复制有多种方法,其中有一种就是System.arraycopy方法,传闻速度也很快. 方法完整签名: public static void arraycopy(Object src, int s ...
- Solr部署如何启动
我刚接触solr,我要怎么启动,这是群里的朋友问得比较多的问题, solr最新版本下载地址: http://www.apache.org/dyn/closer.cgi/lucene/solr/ 1.准 ...
- logback.xml_appender配置
logback<appender> <appender>: <appender>是<configuration>的子节点,是负责写日志的组件. < ...