简体   繁体   English

ASP.NET MVC,Actionlink

[英]ASP.NET MVC, Actionlink

I am revisiting MVC after some time I am facing some weird errors, which I just can't seem to solve.一段时间后我正在重新访问 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>

This View gives o/p此视图提供 o/p

System.Collections.Generic.List`1[StockEdgeTest.Models.Department] List Department List HR Finance DB IT Management Corpo Bank Facilities Useless Directors System.Collections.Generic.List`1[StockEdgeTest.Models.Department] List Department List HR Finance DB IT Management Corpo Bank Facilities 无用董事

I do not know how to remove this line System.Collections.Generic.List`1[StockEdgeTest.Models.Department] List我不知道如何删除这一行 System.Collections.Generic.List`1[StockEdgeTest.Models.Department] List

More importantly, the actionlink does not seem to working, it just generates https://localhost:44354/?Length=8 instead of https://localhost:44354/Employee/Index更重要的是,actionlink 似乎不起作用,它只是生成 https://localhost:44354/?Length=8 而不是 https://localhost:44354/Employee/Index

any ideas how to address this?任何想法如何解决这个问题? EDIT: action编辑:行动

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);
        }

    }
}

fix your view:修正你的观点:

@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

and in you Index action replace:并在您的索引操作中替换:

return View()

with

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM