博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.Net简单的交互案例
阅读量:5348 次
发布时间:2019-06-15

本文共 7977 字,大约阅读时间需要 26 分钟。

422101-20170519181545275-1327735692.png

422101-20170519181551010-1639446824.png

422101-20170519181555744-1572655122.png

控制器

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using mvcDemo.Models; // 引入Modelsnamespace 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

@{    Layout = null;}    
Index
@ViewBag.Greeting World!

We're going to have an exciting party.

(To do : sell it better. Add pictures or something.)

@Html.ActionLink("RSVP Now","RsvpForm");

RsvpForm

@model mvcDemo.Models.GuestResponse@{    Layout = null;}    
RsvpForm
@using (Html.BeginForm()) {

Your name:@Html.TextBoxFor(x => x.Name)

Your email:@Html.TextBoxFor(x => x.Email)

Your phone:@Html.TextBoxFor(x => x.Phone)

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")

}

Thanks

@{    Layout = null;}    
Thanks

Thank you ,@Model.Name!

@if(Model.WillAttend == true) { @:Its greet that you're coming! }else { @:Sorry to hear that! }

增加验证 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; // 引入Modelsnamespace 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();        }    }}

视图层

@model mvcDemo.Models.GuestResponse@{    Layout = null;}    
RsvpForm
@using (Html.BeginForm()) { @Html.ValidationSummary()

Your name:@Html.TextBoxFor(x => x.Name)

Your email:@Html.TextBoxFor(x => x.Email)

Your phone:@Html.TextBoxFor(x => x.Phone)

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")

}

422101-20170519185332150-1183516798.png

增加样式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;}

引入样式

@model mvcDemo.Models.GuestResponse@{    Layout = null;}    
RsvpForm
@using (Html.BeginForm()) { @Html.ValidationSummary()

Your name:@Html.TextBoxFor(x => x.Name)

Your email:@Html.TextBoxFor(x => x.Email)

Your phone:@Html.TextBoxFor(x => x.Phone)

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")

}

422101-20170522172137788-1359153111.png

增加bootstrap特效

@{    Layout = null;}    
Index

We're going to have an exciting party!

And you are invited

@Html.ActionLink("RSVP Now", "RsvpForm")
@model mvcDemo.Models.GuestResponse@{    Layout = null;}    
RsvpForm

RSVP

@using (Html.BeginForm()) { @Html.ValidationSummary()
@Html.TextBoxFor(x => x.Name, new { @class = "form-control" })
@Html.TextBoxFor(x => x.Email, new { @class = "form-control" })
@Html.TextBoxFor(x => x.Phone, new { @class = "form-control" })
@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" })
}

422101-20170522181138538-371674567.png

422101-20170522181148429-502292283.png

增加邮件发送提醒

@{    Layout = null;}    
Thanks
@{ 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) { @: Sorry - Can't send Email } }

Thank you ,@Model.Name!

@if (Model.WillAttend == true) { @:Its greet that you're coming! } else { @:Sorry to hear that! }

转载于:https://www.cnblogs.com/jiqing9006/p/6879818.html

你可能感兴趣的文章
第06篇 MEF部件的生命周期(PartCreationPolicy)
查看>>
Django 基础 web框架本质
查看>>
25 python socket网络编程
查看>>
sprint
查看>>
tensorflow的gpu版本错误
查看>>
接口和抽象类有何不同?
查看>>
WinForm——记住密码
查看>>
[转载]令人眼花缭乱的图形:Windows Presentation Foundation 中的十大 UI特性
查看>>
python处理大文件——文件流处理
查看>>
英语音标双元音篇
查看>>
旷视研究院Detection组负责人
查看>>
优化Web中的性能
查看>>
180130 猜数字游戏的改进
查看>>
计算广告 互联网商业变现的市场与技术.pdf
查看>>
About Face 3:交互设计精髓pdf
查看>>
css揭秘pdf
查看>>
第3讲:运算符
查看>>
Codeforces Round #150 (Div. 2) B dfs
查看>>
Find Peak Element
查看>>
利用GeoWebCache实现WebGIS地形图展示的缓存优化
查看>>