2. add a view to ASP.NET Core MVC application

In this section, the modification HelloWorldController classes, further using Razor view of the package file to smoothly process the HTML response to the client-side generated.

Current, Index method returns the string with the message hard-coded in the controller class. In HelloWorldController class, the Index method with the following code:

 


public IActionResult Index()
{
return View();
}

 

 


The above method of code calls View Controller. It uses the template to generate HTML response view. A controller process (also referred to as "operation method", such as the Index above method)
generally returns IActionResult (or derived from ActionResult class), but not other types of string.

 

 

Add View


Right-click the "View" folder, then click the "Add"> "New Folder", and the folder is named " the HelloWorld ."
Right-click the "Views / HelloWorld" folder, then click the "Add"> "New Item."
In the "Add New Item - MvcMovie" dialog box
in the upper right corner of the search box, type "view"
select "Razor View"
to maintain the "Name" box value: Index.cshtml.
Select "Add"

 

Razor view file contents replaced Views / HelloWorld / Index.cshtml with the following content:


@{
ViewData["Title"] = "Index";
}

<h2>Index</h2>

<p>Hello from our View Template!</p>

 

 

Navigate to https: // localhost: xxxx / HelloWorld . Index method HelloWorldController role is not; it runs return View ();
statement, specify this method should be used to render the view template file a response to the browser. Because not explicitly specify the name of the view template file, so the MVC default
Index.cshtml view file / Views / HelloWorld folder. The following image shows the view of the hard-coded string "Hello from our View Template!"


Change the view and layout of the page


Select the menu links ( "MvcMovie", "Home" and "privacy"). Per the same menu layout. Menu layout is implemented in the Views / Shared / _Layout.cshtml file.
Open the Views / Shared / _Layout.cshtml file.
Layout templates allow you to specify the layout of the site's HTML container in a location, and then apply it to multiple pages of your website. Find @RenderBody () line.
RenderBody is specific to view all the pages placeholders show created, packaged in the layout page. For example, if you select the "Privacy" link
, Views / Home / Privacy.cshtml view will be presented RenderBody method.

Change the layout file header, footer and menu links
in the header and footer elements, the MvcMovie change Movie App.
The anchor element


<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">MvcMovie</a>
更改为 <a class="navbar-brand" asp-controller="Movies" asp-action="Index">Movie App</a>



In front of the tag is omitted asp-area anchor tag helper properties, this application because the unused area.
Description: Movies controller has not been implemented. At this point, Movie App link does not work.

 


Complete HelloWorldController.cs file looks like this:


using Microsoft.AspNetCore.Mvc;
using System.Text.Encodings.Web;

namespace MvcMovie.Controllers
{
public class HelloWorldController : Controller
{
public IActionResult Index()
{
return View();
}

public IActionResult Welcome(string name, int numTimes = 1)
{
ViewData["Message"] = "Hello " + name;
ViewData["NumTimes"] = numTimes;

return View();
}
}
}

 

 

Create a file called Views / HelloWorld / Welcome.cshtml the Welcome view template.
Replace the contents of Views / HelloWorld / Welcome.cshtml with the following content:

 

@{
ViewData["Title"] = "Welcome";
}

<h2>Welcome</h2>

<ul>
@for (int i = 0; i < (int)ViewData["NumTimes"]; i++)
{
<li>@ViewData["Message"]</li>
}
</ul>

 

 

Save the changes and browse to the following URL:


https://localhost:xxxx/HelloWorld/Welcome?name=Rick&numtimes=4

 

Data taken from the URL, and passed to the controller using the MVC model binder. The packed data to controller ViewData dictionary, and pass the object to the view.
Then, view the data presented as HTML to the browser.
In the example above, we use the ViewData dictionary data transmitted from the controller to the view.

Guess you like

Origin www.cnblogs.com/ouyangkai/p/10937849.html