其实有三个方法用于传递information
- 使用 strongly typed model object
- 使用 ViewBag
- 使用 dynamic type (using @model dynamic)
本来要用自己的例子,但是找到几个非常有趣的例子,来自于《Pro asp.net mvc 4 4th edition》,这里做一个引用。
关于strongly typed model object,请参考:
http://www.asp.net/mvc/tutorials/views/dynamic-v-strongly-typed-views
或《Pro asp.net mvc 4 4th edition》31页
关于ViewBag,它其实是一个dynamic object可以用于赋值。
以下代码源于《Pro asp.net mvc 4 4th edition》26页
Controller:
1 public class HomeController : Controller {
2 public ViewResult Index() {
3 int hour = DateTime.Now.Hour;
4 ViewBag.Greeting = hour < 12 ? "Good Morning" : "Good Afternoon";
5 return View();
6 }
7 }
2 public ViewResult Index() {
3 int hour = DateTime.Now.Hour;
4 ViewBag.Greeting = hour < 12 ? "Good Morning" : "Good Afternoon";
5 return View();
6 }
7 }
View:
1 @{
2 Layout = null;
3 }
4 <!DOCTYPE html>
5 <html>
6 <head>
7 <meta name="viewport" content="width=device-width" />
8 <title>Index</title>
9 </head>
10 <body>
11 <div>
12 @ViewBag.Greeting World (from the view)
13 </div>
14 </body>
15 </html>
2 Layout = null;
3 }
4 <!DOCTYPE html>
5 <html>
6 <head>
7 <meta name="viewport" content="width=device-width" />
8 <title>Index</title>
9 </head>
10 <body>
11 <div>
12 @ViewBag.Greeting World (from the view)
13 </div>
14 </body>
15 </html>
关于用dynamic type (using @model dynamic)
以下代码子源于《Pro asp.net mvc 4 4th edition》35页
Model:
1 namespace PartyInvites.Models {
2 public class GuestResponse {
3 public string Name { get; set; }
4 public string Email { get; set; }
5 public string Phone { get; set; }
6 public bool? WillAttend { get; set; }
7 }
8 }
2 public class GuestResponse {
3 public string Name { get; set; }
4 public string Email { get; set; }
5 public string Phone { get; set; }
6 public bool? WillAttend { get; set; }
7 }
8 }
其中WillAttend为nullable bool,也就是它的值可以为true,false或者为null。
Controller:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6 using PartyInvites.Models;
7 namespace PartyInvites.Controllers {
8 public class HomeController : Controller {
9 public ViewResult Index() {
10 int hour = DateTime.Now.Hour;
11 ViewBag.Greeting = hour < 12 ? "Good Morning" : "Good Afternoon";
12 return View();
13 }
14 [HttpGet]
15 public ViewResult RsvpForm() {
16 return View();
17 }
18 [HttpPost]
19 public ViewResult RsvpForm(GuestResponse guestResponse) {
20 // TODO: Email response to the party organizer
21 return View("Thanks", guestResponse);
22 }
23 }
24 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6 using PartyInvites.Models;
7 namespace PartyInvites.Controllers {
8 public class HomeController : Controller {
9 public ViewResult Index() {
10 int hour = DateTime.Now.Hour;
11 ViewBag.Greeting = hour < 12 ? "Good Morning" : "Good Afternoon";
12 return View();
13 }
14 [HttpGet]
15 public ViewResult RsvpForm() {
16 return View();
17 }
18 [HttpPost]
19 public ViewResult RsvpForm(GuestResponse guestResponse) {
20 // TODO: Email response to the party organizer
21 return View("Thanks", guestResponse);
22 }
23 }
24 }
注意的是HttpGet attribute和HttpPost attribute,告诉method是处理get还是post。
View: RsvpForm.cshtml
1 @model PartyInvites.Models.GuestResponse
2 @{
3 Layout = null;
4 }
5 <!DOCTYPE html>
6 <html>
7 <head>
8 <meta name="viewport" content="width=device-width" />
9 <title>RsvpForm</title>
10 </head>
11 <body>
12 @using (Html.BeginForm()) {
13 <p>Your name: @Html.TextBoxFor(x => x.Name) </p>
14 <p>Your email: @Html.TextBoxFor(x => x.Email)</p>
15 <p>Your phone: @Html.TextBoxFor(x => x.Phone)</p>
16 <p>
17 Will you attend?
18 @Html.DropDownListFor(x => x.WillAttend, new[] {
19 new SelectListItem() {Text = "Yes, I'll be there",
20 Value = bool.TrueString},
21 new SelectListItem() {Text = "No, I can't come",Value = bool.FalseString}
22 }, "Choose an option")
23 </p>
24 <input type="submit" value="Submit RSVP" />
25 }
26 </body>
27 </html>
2 @{
3 Layout = null;
4 }
5 <!DOCTYPE html>
6 <html>
7 <head>
8 <meta name="viewport" content="width=device-width" />
9 <title>RsvpForm</title>
10 </head>
11 <body>
12 @using (Html.BeginForm()) {
13 <p>Your name: @Html.TextBoxFor(x => x.Name) </p>
14 <p>Your email: @Html.TextBoxFor(x => x.Email)</p>
15 <p>Your phone: @Html.TextBoxFor(x => x.Phone)</p>
16 <p>
17 Will you attend?
18 @Html.DropDownListFor(x => x.WillAttend, new[] {
19 new SelectListItem() {Text = "Yes, I'll be there",
20 Value = bool.TrueString},
21 new SelectListItem() {Text = "No, I can't come",Value = bool.FalseString}
22 }, "Choose an option")
23 </p>
24 <input type="submit" value="Submit RSVP" />
25 }
26 </body>
27 </html>
View: Thanks.cshtml
1 @model PartyInvites.Models.GuestResponse
2 @{
3 Layout = null;
4 }
5 <!DOCTYPE html>
6 <html>
7 <head>
8 <meta name="viewport" content="width=device-width" />
9 <title>Thanks</title>
10 </head>
11 <body>
12 <div>
13 <h1>Thank you, @Model.Name!</h1>
14 @if (Model.WillAttend == true) {
15 @:It's great that you're coming. The drinks are already in the fridge!
16 } else {
17 @:Sorry to hear that you can't make it, but thanks for letting us know.
18 }
19 </div>
20 </body>
21 </html>
2 @{
3 Layout = null;
4 }
5 <!DOCTYPE html>
6 <html>
7 <head>
8 <meta name="viewport" content="width=device-width" />
9 <title>Thanks</title>
10 </head>
11 <body>
12 <div>
13 <h1>Thank you, @Model.Name!</h1>
14 @if (Model.WillAttend == true) {
15 @:It's great that you're coming. The drinks are already in the fridge!
16 } else {
17 @:Sorry to hear that you can't make it, but thanks for letting us know.
18 }
19 </div>
20 </body>
21 </html>
没有评论:
发表评论