ASP.Net简单的交互案例



控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mvcDemo.Models; // 引入Models
namespace 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
<!DOCTYPE html>
@{
Layout = null;
}
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Index</title>
<link rel="stylesheet" href="">
</head>
<body>
<div>@ViewBag.Greeting World!</div>
<p>We're going to have an exciting party. <br/>
(To do : sell it better. Add pictures or something.)
</p>
@Html.ActionLink("RSVP Now","RsvpForm");
</body>
</html>
RsvpForm
<!DOCTYPE html>
@model mvcDemo.Models.GuestResponse
@{
Layout = null;
}
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>RsvpForm</title>
<link rel="stylesheet" href="">
</head>
<body>
@using (Html.BeginForm())
{
<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 cant come",
Value = bool.FalseString
},
},"Choose an option")
</p>
<input type="submit" value="Submit RSVP">
}
</body>
</html>
Thanks
<!DOCTYPE html>
@{
Layout = null;
}
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Thanks</title>
<link rel="stylesheet" href="">
</head>
<body>
<h1>Thank you ,@Model.Name!</h1>
@if(Model.WillAttend == true)
{
@:Its greet that you're coming!
}else
{
@:Sorry to hear that!
}
</body>
</html>
增加验证 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; // 引入Models
namespace 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();
}
}
}
视图层
<!DOCTYPE html>
@model mvcDemo.Models.GuestResponse
@{
Layout = null;
}
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>RsvpForm</title>
<link rel="stylesheet" href="">
</head>
<body>
@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 cant come",
Value = bool.FalseString
},
},"Choose an option")
</p>
<input type="submit" value="Submit RSVP">
}
</body>
</html>

增加样式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;}
引入样式
<!DOCTYPE html>
@model mvcDemo.Models.GuestResponse
@{
Layout = null;
}
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>RsvpForm</title>
<link rel="stylesheet" href="~/Content/Styles.css">
<link rel="stylesheet" href="">
</head>
<body>
@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 cant come",
Value = bool.FalseString
},
},"Choose an option")
</p>
<input type="submit" value="Submit RSVP">
}
</body>
</html>

增加bootstrap特效
<!DOCTYPE html>
@{
Layout = null;
}
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Index</title>
<link rel="stylesheet" href="~/Content/bootstrap.css">
<link rel="stylesheet" href="~/Content/bootstrap-theme.css">
<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>
<div class="btn btn-success">
@Html.ActionLink("RSVP Now", "RsvpForm")
</div>
</div>
</body>
</html>
<!DOCTYPE html>
@model mvcDemo.Models.GuestResponse
@{
Layout = null;
}
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>RsvpForm</title>
<link rel="stylesheet" href="~/Content/Styles.css">
<link rel="stylesheet" href="~/Content/bootstrap.css">
<link rel="stylesheet" href="~/Content/bootstrap-theme.css">
<link rel="stylesheet" href="">
</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" value="Submit RSVP" />
</div>
}
</div>
</div>
</body>
</html>


增加邮件发送提醒
<!DOCTYPE html>
@{
Layout = null;
}
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Thanks</title>
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<link href="~/Content/bootstrap-theme.css" rel="stylesheet" />
<style>
body {
background-color: #F1F1F1;
}
</style>
</head>
<body>
@{
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)
{
@:<b> Sorry - Can't send Email </b>
}
}
<div class="text-center">
<h1>Thank you ,@Model.Name!</h1>
<div class="lead">
@if (Model.WillAttend == true)
{
@:Its greet that you're coming!
}
else
{
@:Sorry to hear that!
}
</div>
</div>
</body>
</html>
ASP.Net简单的交互案例的更多相关文章
- Asp.net与Dojo交互:仪器仪表实现
项目中需要用到仪器仪表的界面来显示实时的采集信息值,于是便遍地寻找,参考了fusionchart和anychart之后,发现都是收费的,破解的又没有这些功能,只好作罢.之后又找遍了JQuery的插件, ...
- Prolog学习:基本概念 and Asp.net与Dojo交互:仪器仪表实现
Asp.net与Dojo交互:仪器仪表实现 项目中需要用到仪器仪表的界面来显示实时的采集信息值,于是便遍地寻找,参考了fusionchart和anychart之后,发现都是收费的,破解的又没有这些功能 ...
- ajax交互案例
数据交互是前端很重要的一部分,静态页是基础,而交互才是网页的精髓.交互又分为人机交互和前后端数据交互,现阶段的互联网下,大部分的网站都要进行前后端数据交互,如何交互呢?交互的流程大概就是前端发送数据给 ...
- C#仿google日历asp.net简单三层版本
网上搜了很多xgcalendar的例子都是Php开发的,而且官方站上的asp.net/MVC版 在vs10 08 都报错. 所以自己重新用三层写了一下希望对大家有帮助 废话不多说了 先看看它都有些什么 ...
- 用ASP实现简单的繁简转换
用ASP实现简单的繁简转换 国际化似乎是一个很流行的口号了,一个站点没有英文版至少也要弄个繁体版,毕竟都是汉字,翻译起来不会那么麻烦:P 一般的繁简转换是使用字典,通过GB的内码算出BIG5字符在字典 ...
- java--文件过滤器和简单系统交互
一.文件过滤器 /** * @Title: getFileByFilter * @Description: 根据正则rege获取给定路径及其子路径下的文件名(注意递归的深度不要太大) * @param ...
- ajax简单后台交互
ajax简单后台交互 1,扯淡 单身的生活,大部分时间享受自由,小部分时间忍受寂寞. 生活有时候,其实蛮苦涩,让人难以下咽.那些用岁月积累起来的苦闷,无处宣泄,在自己的脑海里蔓延成一片片荆棘,让你每每 ...
- Python3+Dlib实现简单人脸识别案例
Python3+Dlib实现简单人脸识别案例 写在前边 很早很早之前,当我还是一个傻了吧唧的专科生的时候,我就听说过人脸识别,听说过算法,听说过人工智能,并且也出生牛犊不怕虎般的学习过TensorFl ...
- 使用Java实现简单的斗地主案例
使用Java实现简单的斗地主案例 案例说明:使用Java实现简单的斗地主洗牌发牌的操作: 具体规则: 共有54张牌,顺序打乱: 三个玩家参与游戏,三人交替摸牌,每人17张牌,最后留三张为底牌(地主牌) ...
随机推荐
- deque 归纳
deque是STL里面的常见容器,它的本质是一个队列,但是与队列不同是的是,它可以两边进出. 下面是STL的一些常见操作. que.assign(beg,end) 将[beg; end)区间中的数据赋 ...
- BZOJ 2115 DFS+高斯消元
思路: 先搞出来所有的环的抑或值 随便求一条1~n的路径异或和 gauss消元找异或和最大 贪心取max即可 //By SiriusRen #include <cstdio> #inclu ...
- Edge浏览器开发人员工具
UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Ch ...
- 使用U盘安装win8(win8.1)系统
下载镜像 下载系统镜像,可以选32位或64位.下完后放到非系统盘(如果需要重新分区的,要先保存到移动硬盘或U盘) 使用U盘制作PE 准备一个4G以上的U盘(备份好U盘里的资料,因为刻录PE需要格式化U ...
- CF402E Strictly Positive Matrix(矩阵,强联通分量)
题意 给定一个 n∗n 的矩阵 A,每个元素都非负判断是否存在一个整数 k 使得 A^k 的所有元素 >0 n≤2000(矩阵中[i][i]保证为1) 题解 考虑矩阵$A*A$的意义 ,设得到的 ...
- Dia Diagram Editor(流程图、UML)免费开源绘图软件
近期工作各种繁忙,导致很少分享自己喜欢和常用的一些工具,今天有点时间再次给各位喜欢开源的小伙伴介绍一个好用.免费.开源的软件Dia Diagram Editor. 首先给大家看看这个软件的主界面吧! ...
- 从终端运行python程序
终端窗口运行.py程序 首先你要安装python,命令行输入 python 有python提示符 >>> 出现说明安装成功 程序第一行应该是 #! python3 #! python ...
- [TJOI2017]城市(树的直径)
[TJOI2017]城市 题目描述 从加里敦大学城市规划专业毕业的小明来到了一个地区城市规划局工作.这个地区一共有ri座城市,<-1条高速公路,保证了任意两运城市之间都可以通过高速公路相互可达, ...
- [ZJOI2012]旅游(树的直径)
[ZJOI2012]旅游 题目描述 到了难得的暑假,为了庆祝小白在数学考试中取得的优异成绩,小蓝决定带小白出去旅游~~ 经过一番抉择,两人决定将T国作为他们的目的地.T国的国土可以用一个凸N边形来表示 ...
- java中hashmap和hashtable和hashset的区别
hastTable和hashMap的区别:(1)Hashtable是基于陈旧的Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现.(2)这个不同即是最重要的一点:Ha ...