1. Use of Razor Helpers:
For example, define ViewBag. Message = "Welcome to ASP. net mvc! "; I want to display it on the Interface
"Welcome..."; we generally have two types of operations and three implementation operations:
Two operations:
Extension Method off HtmlHelpers and Razor Declarative @ Helper Sytnax
Three implementation methods:
1. Extension Method
Create a folder named Helpers under the current project and add the HtmlHelpers class to the folder. The specific implementation is as follows:
HtmlHelpers
Namespace MVCET. Helpers
{
Public static class HtmlHelpers
{
Public static string Truncat (this HtmlHelper helper, string input, int length)
{
If (input. Length <= length)
{
Return input;
}
Else
{
Return input. Substring (0, length) + "...";
}
}
}
}
At this time, you only need to add such code on the page to display it:
@ Using MVCET. Helpers
<H2> @ Html. Truncat (@ ViewBag. Message as string, 8)
2. Razor Declarative @ Helper Sytnax
1. Add the following code on the current page:
@ Helper Truncat (string input, int length)
{
If (input. Length <= length)
{
@ Input
}
Else
{
@ Input. Substring (0, length) <text>... (Razor) </text>
}
}
Add the following line of code:
<H2> @ Truncat (@ ViewBag. Message as string, 8)
The displayed result is exactly the same as the above.
2. Add the App_Code folder and add the RazorHelper. cshtml Razor file. The statement is as follows:
@ Helper Truncat (string input, int length)
{
If (input. Length <= length)
{
@ Input
}
Else
{
@ Input. Substring (0, length) <text>... (Razor) </text>
}
}
Add the following code on the page:
<H2> @ RazorHelper. Truncat (@ ViewBag. Message, 8) Run, we can see that the results are the same.
-----------------------------------------------------------------
2. query with parameters using Linq
If we add parameters to the Index page, we can pass them parameters in many ways to respond to events. For example, in the following example, you can use a shortcut to view the Restaurant of Shanghai.
First: @ Html. ActionLink ()
Add the following code to RestaurantControl:
OdeToFoodDB _ db = new OdeToFoodDB ();
Public ActionResult Index (string city)
{
Var model = from r in _ db. Distributed ants
Where r. Adress. City = city | (city = null)
Orderby r. Name
Select r;
Return View (model );
}
In the View of the Restaurant, write the code on the Index page:
<P> @ Html. ActionLink ("To see Restaurant in shanghai", "Index", "Restaurant", new {city = "Shanghai"}, null) </p>
Type 2: bind a field
Add the DownloadList to allow the DownloadList to be freely selected using the options. DownloadList can bind fields.
Add the following code to RestaurantControl:
OdeToFoodDB _ db = new OdeToFoodDB ();
Public ActionResult Index (string city)
{
ViewBag. City = _ db. Sort ants. Select (r => r. Adress. City). Distinct ();
Var model = from r in _ db. Distributed ants
Where r. Adress. City = city | (city = null)
Orderby r. Name
Select r;
// Var model = _ db. mongoants
//. Where (r => r. Adress. City = "Guangdong ")
//. OrderBy (r => r. Name );
Return View (model );
}
In the View of the Restaurant, write the code on the Index page:
@ Using (Html. BeginForm ("Index", "Restaurant", FormMethod. Get ))
{
@ Html. DropDownList ("City", new SelectList (ViewBag. City ))
<Input type = "submit" value = "Filter"/>
}
Here, I bound DropDownList to a dynamic type (ViewBag. City.