官术网_书友最值得收藏!

How to do it...

  1. Open Visual Studio 2017.
  2. Now, open the solution from the previous recipe. Click File | Open | Open Project/Solution, or press Ctrl + Shift + O, and select the Chapter2.Linq solution. 
  3. Press Ctrl + Shift + B for a quick build to check that everything is fine. 
  4. Now, click on the Chapter2.Linq solution label. Click File | Add | New Project.
  5. In the Add New Project template dialog box, expand the Visual C# node in the left-hand pane. 
  6. Select Web and then ASP.NET Web Application (.NET Framework) in the right-hand pane:
  1. Now, in the Name: text box, type Chapter2.Linq.QueriesMVC as the name and leave the Location: text box at its default value:
  1. In the New ASP.NET Web Application dialog box, select Empty from the template list.
  1. Select MVC as the Add folders and core references for: option:
  1. Leave the rest as it is and click OK to create the default ASP.NET MVC Web Application template. 
  1. Now, Solution Explorer should look like this:
  1. Now, right-click on the Controllers folder inside the Chapter2.Linq.QueriesMVC project. 
  2. Select Add | Controller
  1. In the Add Scaffold dialog box, select MVC 5 Controller - Empty:
  1. Click Add.
  1. In the Add Controller dialog box, type HomeController as the name of the controller:
  1. Click Add.
  2. Now, the Solution Explorer (press Ctrl + Alt + L) should look like this:
  1. Now, right-click on the References label and select Add Reference.
  2. In the Reference Manager, select Projects and check Chapter2.Linq.QueriesLib from the list:
  1. Click OK
  2. Now, double-click on HomeController.cs in the Controllers folder. 
  3. In the code window for HomeController.cs, scroll up and add the following code at the last line of the using directives: 
      using Chapter2.Linq.QueriesLib;
  1. Inside the Index() action, before the return keyword, add the following code: 
      var telephoneBook = new TelephoneBook();

ViewBag.Contacts = telephoneBook.GetContacts();
  1. Right-click on the method name of the Index() method and select Add View:
  1. In the Add View dialog box, leave the defaults and click Add:
  1. You will be presented with an Index.cshtml code window with the default template. 
  2. Change the code <h2>Index</h2> as follows:
      <h2>Contacts</h2>
  1. Now move the cursor to the bottom of the code window and add this code:
      <ul>
@foreach(var contact in ViewBag.Contacts as List<string>)
{
<li>@contact</li>
}
</ul>
  1. Let's press F5 and test the code. 
  1. By default, the browser will load http://localhost:portnumber/Home/ and here is the output: 
  1. Now let's close the browser and switch back to the HomeController.cs code window.
  2. Right after the end curly bracket of the Index() action method, add the following code: 
      public ActionResult Search(string ln)
{

var telephoneBook = new TelephoneBook();

if (string.IsNullOrEmpty(ln))
{
ViewBag.Contacts = telephoneBook.GetContacts();
}
else
{
ViewBag.Contacts =
telephoneBook.GetContactsByLastName(ln);

}

return View();
}
  1. Right-click on the method name of the Search() action and select Add View.
  2. Follow steps 26 and 27 to add the Search.chtml view. 
  3. In the Search.chtml code window, change the code <h2>Search</h2> to the following : 
      <h2>Search Results - Contacts</h2>
  1. Add the following code to Search.chtml after the <h2> tags:
      <ul>
@foreach (var contact in ViewBag.Contacts as List<string>)
{
<li>@contact</li>
}
</ul>
  1. Let's press F5 and debug the current code. 
  2. Type http://localhost:portnumber/Home/Search in the browser address bar and press Enter:
  1. Now again, type http://localhost:portnumber/Home/Search?ln=Marrier in the address bar and press Enter
  1. Close the browser and switch back to the HomeController.cs code window.
  2. Add the following code after the Search() action method: 
      public ActionResult SortedContacts(bool asc = true)
{
var telephoneBook = new TelephoneBook();

ViewBag.Contacts = telephoneBook.GetSortedContacts(asc);

return View();
}
  1. Right-click on the method name of the SortedContacts() action and select Add View.
  2. Follow steps 26 and 27 to add the SortedContacts.chtml view. 
  1. Now, in the SortedContacts.chtml, change <h2>SortedContacts</h2> to <h2>Sorted Contacts</h2>.
  2. At the end of the <h2> tags add the following code.
      <ul>
@foreach (var contact in ViewBag.Contacts as List<string>)
{
<li>@contact</li>
}
</ul>
  1. Press F5 to debug the current code. 
  2. Type http://localhost:portnumber/Home/SortedContacts in the address bar of the browser and press Enter:
  1. Again, type http://localhost:51084/Home/SortedContacts?asc=false in the address bar and press Enter:
  1. Close the browser. 
主站蜘蛛池模板: 兴海县| 沂水县| 宁强县| 长垣县| 静安区| 伊宁县| 孝昌县| 宜丰县| 怀柔区| 石泉县| 筠连县| 漳州市| 前郭尔| 墨玉县| 北海市| 腾冲县| 淄博市| 金昌市| 平陆县| 广水市| 西畴县| 历史| 庆云县| 商丘市| 称多县| 天祝| 如东县| 钟祥市| 张家口市| 栖霞市| 永丰县| 海盐县| 固阳县| 东乌珠穆沁旗| 彝良县| 湘乡市| 闵行区| 七台河市| 通许县| 太康县| 金坛市|