MVC4入门1(基于一定基础上最起码能建mvc4的意见的项目)

首先创建一个简单的mvc4的项目

代码里面有解释比较清楚 就不过多解释

简单来说基本目录跟我的一样 mvc中提倡约定高于配置

ViewBag 表示一种动态对象 类似于Object 可以赋值任意属性 Greeting 随意命名
作用可以将后台某些属性传递到View页面中

 @ViewBag.Greeting World;(进行页面渲染 试图引擎将Greeting提取出来)

根据HtmlActionLink()方法 我们需要在home控制器中创建一个ResvpForm动作

       [HttpGet] //默认情况下是get  好像是
        public ActionResult ResvpForm()
        {

            return View();
        }

  贴上相应视图代码  创建一个强类型的视图创建前先贴上model类的代码

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace testOne.Models
{
    public class GuestReponse
    {
        [Required(ErrorMessage="请输入你姓名")]  //需要导dll文件 using System.ComponentModel.DataAnnotations;
                     //模型注释 表示该元素是给模型必需的元素
        public String Name { get; set; }
        [Required(ErrorMessage="请输入你的邮箱")]
        [RegularExpression(".+\\@.+\\..+",ErrorMessage="你输入的邮箱不符合格式")]  //简单的邮箱格式验证 \\相当与转义符
        public String Email { get; set; }
        [Required(ErrorMessage="请输入的电话")]
        public String Phone { get; set; }
        [Required(ErrorMessage="请选择你的选择")]    //之所以使用可空类型 是因为bool 只有true与false二个值 如果没有选择直接提交返回一个null 会导致语法错误
        //bool?  表示可空值类型 原型:Nullable<bool> 编译器简化
        public bool? willAttend { get; set; }
        
    }
}

视图代码

@model testOne.Models.GuestReponse
@{
    //创建一个强类型视图  与普通视图相比  就是多了一个@model 引用 注意是小写m
}

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>ResvpForm</title>
</head>
<body>
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary();  <label>表示验证信息集合展现的地方</label>
        <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 can't come",
            Value=bool.FalseString}
       },"Chose an option");
            @{
                //二个参数 第一个参数表示lambda 表达式绑定属性
                //第二个参数 表示 SelectListItem数组对象 也可以通过后台创建SelectListItem数组对象 通过ViewBag 传输到前端
            }
        </p>
        <input type="submit" value="Submit RSVP" />
    }
</body>
</html>

贴上提交后动作代码

  [HttpPost]  //注解属性  表示只处理post请求
        public ActionResult ResvpForm(GuestReponse guestreponse)
        {
            if (ModelState.IsValid)  //再次进行数据验证 防止非法操(佛说:不能相信客户端发来的任何数据 就算你前端验证写的再完美) 返回true表示通过验证
            {
                return View("Thanks", guestreponse);
            }
            else
            {
                return View();
            }
            
        }

贴上Tanks视图代码

@model testOne.Models.GuestReponse

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Thanks</title>
</head>
<body>
    <div>
        @{
            try
            {
                WebMail.SmtpServer = "smtp.example.com";
                WebMail.SmtpPort = 587;
                WebMail.EnableSsl = true;
                WebMail.UserName = "88888@qq.com";
                WebMail.Password = "88888888";
                WebMail.From = "888@qq.com";
                WebMail.Send("8@qq.com", "尝试", "第一次  试试");
                //是能用 百度能查到相应的参数  但是qq邮箱的真麻烦 麻烦 
            }
        }
        <h1>@Model.Name</h1> (调用的强类型数据 使用大写的M 的Model)
        @if (Model.willAttend==true)
        {
            @:It's greate that you're coming,The drinks are already in the fridge!
        }
        else
        {
            @:Sorry to hear that you can't make it,but thanks for lettin us know
        }
    </div>
</body>
</html>

 

 

除了邮箱 。。。。。。。。

 

posted @ 2016-05-09 23:00  winds_随风  阅读(196)  评论(0)    收藏  举报