控制器

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using mvcDemo.Models; // 引入Models
  7. namespace mvcDemo.Controllers
  8. {
  9. public class HomeController : Controller
  10. {
  11. public ActionResult Index()
  12. {
  13. int hour = DateTime.Now.Hour;
  14. ViewBag.Greeting = hour < 12 ? "Good Morning" : "Goods Afternoon";
  15. return View();
  16. }
  17. [HttpGet]
  18. public ActionResult RsvpForm()
  19. {
  20. return View();
  21. }
  22. [HttpPost]
  23. public ActionResult RsvpForm(GuestResponse guestResponse)
  24. {
  25. return View("Thanks", guestResponse);
  26. }
  27. }
  28. }

models

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. namespace mvcDemo.Models
  6. {
  7. public class GuestResponse
  8. {
  9. public string Name { get; set; }
  10. public string Email { get; set; }
  11. public string Phone { get; set; }
  12. public bool ? WillAttend { get; set; }
  13. }
  14. }

3.视图Index

  1. <!DOCTYPE html>
  2. @{
  3. Layout = null;
  4. }
  5. <html>
  6. <head>
  7. <meta charset="utf-8">
  8. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  9. <title>Index</title>
  10. <link rel="stylesheet" href="">
  11. </head>
  12. <body>
  13. <div>@ViewBag.Greeting World!</div>
  14. <p>We're going to have an exciting party. <br/>
  15. (To do : sell it better. Add pictures or something.)
  16. </p>
  17. @Html.ActionLink("RSVP Now","RsvpForm");
  18. </body>
  19. </html>

RsvpForm

  1. <!DOCTYPE html>
  2. @model mvcDemo.Models.GuestResponse
  3. @{
  4. Layout = null;
  5. }
  6. <html>
  7. <head>
  8. <meta charset="utf-8">
  9. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  10. <title>RsvpForm</title>
  11. <link rel="stylesheet" href="">
  12. </head>
  13. <body>
  14. @using (Html.BeginForm())
  15. {
  16. <p>Your name:@Html.TextBoxFor(x => x.Name)</p>
  17. <p>Your email:@Html.TextBoxFor(x => x.Email)</p>
  18. <p>Your phone:@Html.TextBoxFor(x => x.Phone)</p>
  19. <p>
  20. Will you attend?
  21. @Html.DropDownListFor(x=>x.WillAttend,new[] {
  22. new SelectListItem() {Text = "Yes,I'll be there",
  23. Value = bool.TrueString
  24. },new SelectListItem() {Text = "No,I cant come",
  25. Value = bool.FalseString
  26. },
  27. },"Choose an option")
  28. </p>
  29. <input type="submit" value="Submit RSVP">
  30. }
  31. </body>
  32. </html>

Thanks

  1. <!DOCTYPE html>
  2. @{
  3. Layout = null;
  4. }
  5. <html>
  6. <head>
  7. <meta charset="utf-8">
  8. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  9. <title>Thanks</title>
  10. <link rel="stylesheet" href="">
  11. </head>
  12. <body>
  13. <h1>Thank you ,@Model.Name!</h1>
  14. @if(Model.WillAttend == true)
  15. {
  16. @:Its greet that you're coming!
  17. }else
  18. {
  19. @:Sorry to hear that!
  20. }
  21. </body>
  22. </html>

增加验证 Models

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.ComponentModel.DataAnnotations;// 增加验证引入
  6. namespace mvcDemo.Models
  7. {
  8. public class GuestResponse
  9. {
  10. [Required(ErrorMessage = "请输入姓名")]
  11. public string Name { get; set; }
  12. [Required(ErrorMessage = "请输入邮箱")]
  13. [RegularExpression(".+\\@.+\\..+",ErrorMessage = "请输入正确的邮箱")]
  14. public string Email { get; set; }
  15. [Required(ErrorMessage = "请输入号码")]
  16. public string Phone { get; set; }
  17. [Required(ErrorMessage = "请确认是否参加")]
  18. public bool ? WillAttend { get; set; }
  19. }
  20. }

控制器

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using mvcDemo.Models; // 引入Models
  7. namespace mvcDemo.Controllers
  8. {
  9. public class HomeController : Controller
  10. {
  11. public ActionResult Index()
  12. {
  13. int hour = DateTime.Now.Hour;
  14. ViewBag.Greeting = hour < 12 ? "Good Morning" : "Goods Afternoon";
  15. return View();
  16. }
  17. [HttpGet]
  18. public ActionResult RsvpForm()
  19. {
  20. return View();
  21. }
  22. [HttpPost]
  23. public ActionResult RsvpForm(GuestResponse guestResponse)
  24. {
  25. if (ModelState.IsValid) {
  26. return View("Thanks", guestResponse);
  27. } else
  28. {
  29. return View();
  30. }
  31. }
  32. public ActionResult About()
  33. {
  34. return View();
  35. }
  36. public ActionResult Contact()
  37. {
  38. return View();
  39. }
  40. }
  41. }

视图层

  1. <!DOCTYPE html>
  2. @model mvcDemo.Models.GuestResponse
  3. @{
  4. Layout = null;
  5. }
  6. <html>
  7. <head>
  8. <meta charset="utf-8">
  9. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  10. <title>RsvpForm</title>
  11. <link rel="stylesheet" href="">
  12. </head>
  13. <body>
  14. @using (Html.BeginForm())
  15. {
  16. @Html.ValidationSummary()
  17. <p>Your name:@Html.TextBoxFor(x => x.Name)</p>
  18. <p>Your email:@Html.TextBoxFor(x => x.Email)</p>
  19. <p>Your phone:@Html.TextBoxFor(x => x.Phone)</p>
  20. <p>
  21. Will you attend?
  22. @Html.DropDownListFor(x=>x.WillAttend,new[] {
  23. new SelectListItem() {Text = "Yes,I'll be there",
  24. Value = bool.TrueString
  25. },new SelectListItem() {Text = "No,I cant come",
  26. Value = bool.FalseString
  27. },
  28. },"Choose an option")
  29. </p>
  30. <input type="submit" value="Submit RSVP">
  31. }
  32. </body>
  33. </html>

增加样式Content/Styles.css

  1. .field-validation-error {color: #f00;}
  2. .field-validation-valid { display: none;}
  3. .input-validation-error { border: 1px solid #f00; background-color: #fee; }
  4. .validation-summary-errors { font-weight: bold; color: #f00;}
  5. .validation-summary-valid { display: none;}

引入样式

  1. <!DOCTYPE html>
  2. @model mvcDemo.Models.GuestResponse
  3. @{
  4. Layout = null;
  5. }
  6. <html>
  7. <head>
  8. <meta charset="utf-8">
  9. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  10. <title>RsvpForm</title>
  11. <link rel="stylesheet" href="~/Content/Styles.css">
  12. <link rel="stylesheet" href="">
  13. </head>
  14. <body>
  15. @using (Html.BeginForm())
  16. {
  17. @Html.ValidationSummary()
  18. <p>Your name:@Html.TextBoxFor(x => x.Name)</p>
  19. <p>Your email:@Html.TextBoxFor(x => x.Email)</p>
  20. <p>Your phone:@Html.TextBoxFor(x => x.Phone)</p>
  21. <p>
  22. Will you attend?
  23. @Html.DropDownListFor(x=>x.WillAttend,new[] {
  24. new SelectListItem() {Text = "Yes,I'll be there",
  25. Value = bool.TrueString
  26. },new SelectListItem() {Text = "No,I cant come",
  27. Value = bool.FalseString
  28. },
  29. },"Choose an option")
  30. </p>
  31. <input type="submit" value="Submit RSVP">
  32. }
  33. </body>
  34. </html>

增加bootstrap特效

  1. <!DOCTYPE html>
  2. @{
  3. Layout = null;
  4. }
  5. <html>
  6. <head>
  7. <meta charset="utf-8">
  8. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  9. <title>Index</title>
  10. <link rel="stylesheet" href="~/Content/bootstrap.css">
  11. <link rel="stylesheet" href="~/Content/bootstrap-theme.css">
  12. <style>
  13. .btn a {
  14. color: white;
  15. text-decoration: none;
  16. }
  17. body {
  18. background-color: #F1F1F1;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="text-center">
  24. <h2>We're going to have an exciting party!</h2>
  25. <h3>And you are invited</h3>
  26. <div class="btn btn-success">
  27. @Html.ActionLink("RSVP Now", "RsvpForm")
  28. </div>
  29. </div>
  30. </body>
  31. </html>
  1. <!DOCTYPE html>
  2. @model mvcDemo.Models.GuestResponse
  3. @{
  4. Layout = null;
  5. }
  6. <html>
  7. <head>
  8. <meta charset="utf-8">
  9. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  10. <title>RsvpForm</title>
  11. <link rel="stylesheet" href="~/Content/Styles.css">
  12. <link rel="stylesheet" href="~/Content/bootstrap.css">
  13. <link rel="stylesheet" href="~/Content/bootstrap-theme.css">
  14. <link rel="stylesheet" href="">
  15. </head>
  16. <body>
  17. <div class="panel panel-success">
  18. <div class="panel-heading text-center"><h4>RSVP</h4></div>
  19. <div class="panel-body">
  20. @using (Html.BeginForm())
  21. {
  22. @Html.ValidationSummary()
  23. <div class="form-group">
  24. <label>Your name:</label>
  25. @Html.TextBoxFor(x => x.Name, new { @class = "form-control" })
  26. </div>
  27. <div class="form-group">
  28. <label>Your email:</label>
  29. @Html.TextBoxFor(x => x.Email, new { @class = "form-control" })
  30. </div>
  31. <div class="form-group">
  32. <label>Your phone:</label>
  33. @Html.TextBoxFor(x => x.Phone, new { @class = "form-control" })
  34. </div>
  35. <div class="form-group">
  36. <label>Will you attend?</label>
  37. @Html.DropDownListFor(x => x.WillAttend, new[] {
  38. new SelectListItem() {Text = "Yes, I'll be there",
  39. Value = bool.TrueString},
  40. new SelectListItem() {Text = "No, I can't come",
  41. Value = bool.FalseString}
  42. }, "Choose an option", new { @class = "form-control" })
  43. </div>
  44. <div class="text-center">
  45. <input class="btn btn-success" type="submit" value="Submit RSVP" />
  46. </div>
  47. }
  48. </div>
  49. </div>
  50. </body>
  51. </html>



增加邮件发送提醒

  1. <!DOCTYPE html>
  2. @{
  3. Layout = null;
  4. }
  5. <html>
  6. <head>
  7. <meta charset="utf-8">
  8. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  9. <title>Thanks</title>
  10. <link href="~/Content/bootstrap.css" rel="stylesheet" />
  11. <link href="~/Content/bootstrap-theme.css" rel="stylesheet" />
  12. <style>
  13. body {
  14. background-color: #F1F1F1;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. @{
  20. try
  21. {
  22. WebMail.SmtpServer = "smtp.aliyun.com";
  23. WebMail.SmtpPort = 25;
  24. WebMail.EnableSsl = false;
  25. WebMail.UserName = "diandodo@aliyun.com";
  26. WebMail.Password = "xxxxxxxx";
  27. WebMail.From = "diandodo@aliyun.com";
  28. WebMail.Send("jiqing9006@126.com","RSVP Notification",Model.Name +" is "+ ((Model.WillAttend??false)?"":"not") +"attending" );
  29. }
  30. catch(Exception)
  31. {
  32. @:<b> Sorry - Can't send Email </b>
  33. }
  34. }
  35. <div class="text-center">
  36. <h1>Thank you ,@Model.Name!</h1>
  37. <div class="lead">
  38. @if (Model.WillAttend == true)
  39. {
  40. @:Its greet that you're coming!
  41. }
  42. else
  43. {
  44. @:Sorry to hear that!
  45. }
  46. </div>
  47. </div>
  48. </body>
  49. </html>

ASP.Net简单的交互案例的更多相关文章

  1. Asp.net与Dojo交互:仪器仪表实现

    项目中需要用到仪器仪表的界面来显示实时的采集信息值,于是便遍地寻找,参考了fusionchart和anychart之后,发现都是收费的,破解的又没有这些功能,只好作罢.之后又找遍了JQuery的插件, ...

  2. Prolog学习:基本概念 and Asp.net与Dojo交互:仪器仪表实现

    Asp.net与Dojo交互:仪器仪表实现 项目中需要用到仪器仪表的界面来显示实时的采集信息值,于是便遍地寻找,参考了fusionchart和anychart之后,发现都是收费的,破解的又没有这些功能 ...

  3. ajax交互案例

    数据交互是前端很重要的一部分,静态页是基础,而交互才是网页的精髓.交互又分为人机交互和前后端数据交互,现阶段的互联网下,大部分的网站都要进行前后端数据交互,如何交互呢?交互的流程大概就是前端发送数据给 ...

  4. C#仿google日历asp.net简单三层版本

    网上搜了很多xgcalendar的例子都是Php开发的,而且官方站上的asp.net/MVC版 在vs10 08 都报错. 所以自己重新用三层写了一下希望对大家有帮助 废话不多说了 先看看它都有些什么 ...

  5. 用ASP实现简单的繁简转换

    用ASP实现简单的繁简转换 国际化似乎是一个很流行的口号了,一个站点没有英文版至少也要弄个繁体版,毕竟都是汉字,翻译起来不会那么麻烦:P 一般的繁简转换是使用字典,通过GB的内码算出BIG5字符在字典 ...

  6. java--文件过滤器和简单系统交互

    一.文件过滤器 /** * @Title: getFileByFilter * @Description: 根据正则rege获取给定路径及其子路径下的文件名(注意递归的深度不要太大) * @param ...

  7. ajax简单后台交互

    ajax简单后台交互 1,扯淡 单身的生活,大部分时间享受自由,小部分时间忍受寂寞. 生活有时候,其实蛮苦涩,让人难以下咽.那些用岁月积累起来的苦闷,无处宣泄,在自己的脑海里蔓延成一片片荆棘,让你每每 ...

  8. Python3+Dlib实现简单人脸识别案例

    Python3+Dlib实现简单人脸识别案例 写在前边 很早很早之前,当我还是一个傻了吧唧的专科生的时候,我就听说过人脸识别,听说过算法,听说过人工智能,并且也出生牛犊不怕虎般的学习过TensorFl ...

  9. 使用Java实现简单的斗地主案例

    使用Java实现简单的斗地主案例 案例说明:使用Java实现简单的斗地主洗牌发牌的操作: 具体规则: 共有54张牌,顺序打乱: 三个玩家参与游戏,三人交替摸牌,每人17张牌,最后留三张为底牌(地主牌) ...

随机推荐

  1. 用jquery给select加选中事件

    select在前端开发过程中很常用,现在我们要实现一个效果,那就是选中select中的某一项,执行事件,本来自己没怎么接触过这些,最后网上找了一些资料,自己研究了一下,把方法分享给大家,大家如果有需要 ...

  2. Lamp安装 php-v5.6【ZendGuardLoader】的问题

    Lamp安装 php-v5.6[ZendGuardLoader]的问题 标签(空格分隔):php,linux Apache日志: 就这个问题导致无法解析运行php文件.下面是网上找的解决方案 Zend ...

  3. vuejs v-model

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. codeforces 445 B DZY Loves Chemistry【并查集】

    题意:给出n种化学物质,其中m对会发生化学反应,每次加入化学物质进去的时候, 如果有能够和它发生反应的,危险值就乘以2,问怎样的放入顺序使得危险值最大 将这m对会反应的用并查集处理,统计每个连通块里面 ...

  5. sql sever 创建临时表的两种方法

    创建临时表       方法一:     create table #临时表名( 字段1 约束条件,             字段2 约束条件,                  .....)     ...

  6. vscode 调试vue.js程序

    npm install -g vue-cli                //安装vue-clivue init webpack projectName  //创建项目 1.Ctrl+~ 打开命令行 ...

  7. 前端编程提高之旅(十二)----position置入值应用

    这次内推项目用到的遮罩及其页面下方button都涉及一个概念position置入值得概念.效果图例如以下: 一个元素position属性不是默认值static.那么该元素被称为定位元素. 定位的元素生 ...

  8. Windows 7: Update is not applicable to your computer

    https://www.sevenforums.com/windows-updates-activation/119088-update-not-applicable-your-computer.ht ...

  9. Mysql基础部分,针对以后python使用

    #redis 非关系型数据库#mysql 关系型数据库 表与表之间有数据关系 Oracle Mysql SqlServer DB2#多张表组合在一起就是数据库#冗余 存储两倍数据 可以使系统速度更快 ...

  10. POJ 3281 网络流

    题意: 思路: 网络流 重在建图- 建完了图 就一切都好说了 这道题 我的想法是 先把源点和所有的食品连上边 (容量为1) 再把食品和对应的奶牛连上边 (容量为1) 这个时候要拆点 因为每只奶牛只能才 ...