在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Adding a ViewIn this section we're going to modify the Let's start by using a view template with the public ActionResult Index() { return View(); } Let's now add a view template to our project that we can invoke with the The Add View dialog box appears. Leave the default entries and click the Add button. The MvcMovie/Views/HelloWorld folder and the MvcMovie/Views/HelloWorld/Index.cshtml file are created. You can see them in Solution Explorer:
Add some HTML under the @{ ViewBag.Title = "Index"; } <h2>Index</h2> <b>Hello</b> World! Run the application and browse to the "hello world" controller (http://localhost:xxxx/HelloWorld). The Looks pretty good. However, notice that the browser's title bar says "Index" and the big title on the page says "My MVC Application." Let's change those. Changing views and layout pagesFirst, let's change the text "My MVC Application." That text is shared and appears on every page. It actually appears in only one place in our project, even though it's on every page in our application. Go to the /Views/Shared folder in Solution Explorer and open the _Layout.cshtml file. This file is called a layout page and it's the shared "shell" that all other pages use. Note the <div id="title"> <h1>MVC Movie App</h1> </div> Run the application and note it now says "MVC Movie App". Click the About link, and that page shows "MVC Movie App", too. The complete _Layout.cshtml file is shown below: <!DOCTYPE html> <html> <head> <title>@ViewBag.Title</title> <link href="@Url.Content("/Content/Site.css")" rel="stylesheet" type="text/css" /> <script src="@Url.Content("/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script> </head> <body> <div class="page"> <div id="header"> <div id="title"> <h1>MVC Movie App</h1> </div> <div id="logindisplay"> @Html.Partial("_LogOnPartial") </div> <div id="menucontainer"> <ul id="menu"> <li>@Html.ActionLink("Home", "Index", "Home")</li> <li>@Html.ActionLink("About", "About", "Home")</li> </ul> </div> </div> <div id="main"> @RenderBody() <div id="footer"> </div> </div> </div> </body> </html> Now, let's change the title of the Index page (view). Open MvcMovie/Views/HelloWorld/Index.cshtml. There are two places to make a change: first, the text that appears in the title of the browser, and then in the secondary header (the @{ ViewBag.Title = "Movie List"; } <h2>My Movie List</h2> <b>Hello</b> World! Run the application and browse to http://localhost:xx/HelloWorld. Notice that the browser title, the primary heading, and the secondary headings have changed. It's easy to make big changes in your application with small changes to a view. (If you don't see changes in the browser, you might be viewing cached content. Press Ctrl+F5 in your browser to force the response from the server to be loaded.) Our little bit of "data" (in this case the "Hello World!" message) is hard-coded, though. Our MVC application has V (views) and we've got C (controllers), but no M (model) yet. Shortly, we'll walk through how create a database and retrieve model data from it. Passing Data from the Controller to the ViewBefore we go to a database and talk about models, though, let's first talk about passing information from the Controller to a View. We want to pass what a view template requires in order to render an HTML response to a client. These objects are typically created and passed by a controller class to a view template, and they should contain only the data that the view template requires — and no more. Previously with the Alternatively, we could define a custom class, then create an instance of that object on our own, fill it with data and pass it to the View. That is often called a ViewModel, because it's a custom Model for the View. For small amounts of data, however, the ViewBag works great. Return to the HelloWorldController.cs file change the The complete using System.Web.Mvc; namespace MvcMovie.Controllers { public class HelloWorldController : Controller { public ActionResult Index() { return View(); } public ActionResult Welcome(string name, int numTimes = 1) { ViewBag.Message = "Hello " + name; ViewBag.NumTimes = numTimes; return View(); } } } Now our ViewBag contains data that will be passed over to the View automatically. Again, alternatively we could have passed in our own object like this if we liked: return View(myCustomObject); Now we need a Here's what your Add View dialog box looks like. Add the following code under the @for (int i=0; i < ViewBag.NumTimes;i++) { <h3>@ViewBag.Message @i.ToString()</h3> } Run the application and browse to http://localhost:xx/HelloWorld/Welcome?name=Scott&numtimes=4. Now data is taken from the URL and passed to the controller automatically. The controller packages up the data into a Well, that was a kind of an "M" for model, but not the database kind. Let's take what we've learned and create a database of movies. |
请发表评论