ASP.NET MVC,动作链接

ASP.NET MVC, Actionlink

一段时间后我正在重新访问 MVC,我遇到了一些我似乎无法解决的奇怪错误。

@Model List<StockEdgeTest.Models.Department>
@using StockEdgeTest.Models

@{
    ViewBag.Title = "Department List";
}

<div style="font-family:'Agency FB'">
    <h2>Department List</h2>

    <ul>
        @foreach (Department dep in @Model)
        {
            <li>
                @Html.ActionLink(dep.DepartmentName, "Index", "Employee", new { DepartmentID = Convert.ToInt32(dep.DepartmentID) })
            </li> 
        }

    </ul>
</div>

此视图给出 o/p

System.Collections.Generic.List`1[StockEdgeTest.Models.Department] 列表 部门列表 人力资源部 金融 D B 它 管理 语料库 银行 设施 无用 董事

我不知道如何删除这一行 System.Collections.Generic.List`1[StockEdgeTest.Models.Department] 列表

更重要的是,actionlink 似乎不起作用,它只是生成 https://localhost:44354/?Length=8 而不是 https://localhost:44354/Employee/Index

有什么解决办法吗? 编辑: 动作

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

namespace StockEdgeTest.Controllers
{
    public class DepartmentController : Controller
    {
        // GET: Department
        public ActionResult Index()
        {
            EmployeeContext ecn = new EmployeeContext();
            List<Department> e1 = ecn.Department.ToList();
            return View(e1);
        }

    }
}

修复你的观点:

@model List<StockEdgeTest.Models.Department>

@{
    ViewBag.Title = "Department List";
}

<div style="font-family:'Agency FB'">
    <h2>Department List</h2>

    <ul>
        @foreach (Department dep in @Model)
        {
            <li>
@Html.ActionLink(dep.DepartmentName, "Index", "Employee", new { id = dep.DepartmentID}, null )
            </li> 
        }

    </ul>
</di

并在您的 Index 操作中替换:

return View()

[Route("{id?}")]
public ActionResult Index(int id)
{
    .....
List<StockEdgeTest.Models.Department> model= ...your code
return view(model);
}
```