zoukankan      html  css  js  c++  java
  • 《精通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{displaynone}

    .input-validation-error{ border1px solid #f00;background-color#fee;}

    .validation-summary-errors{ font-weightbold;color#f00}

    .field-validation-valid{displaynone}

  • 相关阅读:
    equals比较对象
    解决打包时出现的Failed to verify bitcode
    苹果的MDM简介
    unable to boot the simulator,无法启动模拟器已解决
    利用NSCalendar类实现日期的比较
    服务器开启https协议
    解决NSTimer存在的内存泄漏的问题
    ipad和iphone的适配
    集成shareSDK错误总结(新浪微博)
    AFN解析器里的坑
  • 原文地址:https://www.cnblogs.com/tangge/p/6263612.html
Copyright © 2011-2022 走看看