29.05.2015 Views

o_19mgorv9t13a3ko71fev19l81mqa.pdf

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

public class HomeController : Controller {<br />

public ViewResult Index() {<br />

int hour = DateTime.Now.Hour;<br />

ViewBag.Greeting = hour < 12 ? "Good Morning" : "Good<br />

Afternoon";<br />

return View();<br />

}<br />

}<br />

}<br />

public ViewResult RsvpForm() {<br />

return View();<br />

}<br />

Adding a Strongly Typed View<br />

I am going to add a view for the RsvpForm action method, but in a slightly different way—I am going to create a strongly<br />

typed view. A strongly typed view is intended to render a specific domain type, and if I specify the type I want to work with<br />

(GuestResponse in this case), MVC can create some helpful shortcuts to make it easier.<br />

Caution Make sure your MVC project is compiled before proceeding. If you have created the GuestResponse class<br />

but not compiled it, MVC won’t be able to create a strongly typed view for this type. To compile your application, select Build<br />

Solution from the Visual Studio Build menu.<br />

Right-click the RsvpForm method in the code editor and select Add View from the pop-up menu to open the Add<br />

View dialog window. Ensure that the View Name is set as RsvpForm, set Template to Empty and select<br />

GuestResponse from the drop-down list for the Model Class field. Leave the View Options boxes<br />

unchecked, as shown in Figure 2-16.<br />

Figure 2-16. Adding a new view to the project<br />

46

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!