2015-02-12 142 views
0

無法加載索引頁面 我是否需要在RouteConfig.cs中進行更改?無法加載頁面

Index.chtml

@model IEnumerable<MVCDemo.Models.Employee> 
@using MVCDemo.Models; 

@{ 
    ViewBag.Title = "Index"; 
} 

<div> 
<h2>Index</h2> 
<ul> 
    @foreach (Employee employee in @Model) 
    { 
     @Html.ActionLink(employee.Name, "Details", new { id = employee.EmployeeId}) 
    } 

</ul> 
</div> 

EmployeeController.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using MVCDemo.Models; 

namespace MVCDemo.Controllers 
{ 
    public class EmployeeController : Controller 
    { 

     public ActionResult Index() 
     { 
      List<Employee> employee = new List<Employee>(); 
      employee.Add(new Employee() 
      { 
       EmployeeId = 101, 
       Name = "John", 
       Gender = "Male", 
       City = "London" 
      }); 

      employee.Add(new Employee() 
      { 
       EmployeeId = 102, 
       Name = "Kacay", 
       Gender = "FeMale", 
       City = "Floria" 
      }); 

      employee.Add(new Employee() 
      { 
       EmployeeId = 103, 
       Name = "Sasha", 
       Gender = "FeMale", 
       City = "USA" 
      }); 

      return View(employee); 
     } 

     public ActionResult Details() 
     { 
      Employee employee = new Employee() 
      { 
       EmployeeId = 101, 
       Name = "John", 
       Gender = "Male", 
       City = "London" 
      }; 

      return View(employee); 
     } 

    } 
} 

Details.chtml

@model MVCDemo.Models.Employee 

@{ 
    ViewBag.Title = "Details"; 
} 

<h2>Details</h2> 

<tr> 
    <td> 
     EmployeeId : 
    </td> 
    <td> 
     @Model.EmployeeId 
    </td> 
    <td> 
     Name : 
    </td> 
    <td> 
     @Model.Name 
    </td> 
    <td> 
     Gender : 
    </td> 
    <td> 
     @Model.Gender 
    </td> 
    <td> 
     City : 
    </td> 
    <td> 
     @Model.City 
    </td> 
</tr> 

Employee.cs(型號)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace MVCDemo.Models 
{ 
    public class Employee 
    { 
     public int EmployeeId { get; set;} 
     public string Name { get; set; } 
     public string Gender { get; set; } 
     public string City { get; set; } 
    } 
} 

無法加載索引頁面 我是否需要在RouteConfig.cs中進行更改?

+0

當你嘗試加載它時會出現什麼錯誤? – krillgar 2015-02-12 03:07:04

回答

0

應該有一個默認的路由配置,應該處理這個。確保將Index.html設置爲正確的文件夾結構。按照慣例,MVC將在某些目錄中查找視圖。在你的情況下,它可能是Views \ Employee \ Index.html

你也可以安裝NuGet軟件包Glimpse,它可以深入瞭解正在發生的事情。

0

您的索引代碼需要一些調整。 試試這個代替索引頁:

@model IEnumerable<MVCDemo.Models.Employee> 
@using MVCDemo.Models; 

@{ 
    ViewBag.Title = "Index"; 
} 
<div> 
<h2>Index</h2> 
<ul> 
    @foreach (var item in Model) 
     { 
     @Html.ActionLink(item.Name, "Details", new { id = item.EmployeeId}) 
    } 
</ul> 
</div>