《精通ASP.NET MVC5》第2章 第一个MVC应用程序

时间:2024-01-11 23:24:38

《精通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{display: none}

.input-validation-error{ border: 1px solid #f00;background-color: #fee;}

.validation-summary-errors{ font-weight: bold;color: #f00}

.field-validation-valid{display: none}