无法加载页面

时间:2015-02-12 03:00:18

标签: asp.net-mvc

无法加载“索引”页面 我是否需要在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(Model)

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中进行更改?

2 个答案:

答案 0 :(得分:0)

应该有一个默认路由配置应该处理这个。确保Index.html设置在正确的文件夹结构中。按照惯例,MVC将在某些目录中查找视图。在你的情况下,它可能是Views \ Employee \ Index.html

您还可以安装NuGet软件包Glimpse,它将深入了解正在发生的事情。

答案 1 :(得分: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>