MVC - DisplayTemplate上的Html.ActionLink无法渲染

时间:2013-10-24 17:50:55

标签: asp.net-mvc asp.net-mvc-routing actionlink

我有以下文件夹结构: 控制器/ EntityController 查看/实体/ Index.cshtml 查看/共享/ DisplaTemplates / CustomEntity.cshtml

EntityController:

public ActionResult Index()
{
    var entities = CustomEntityRepository.GetAll();
    return View(entities);
}

public ActionResult Edit(int id)
{
    if (id == 0)
    {
        return HttpNotFound();
    }
    var entity = CustomEntityRepository.Find(e => e.ID == id);
    return View(entity);
}

Index.cshtml的观点:

@model IEnumerable<CustomEntity>

@{
    ViewBag.Title = "Index";
}
@Html.DisplayForModel()

CustomEntity.cshtml的内容:

@model CustomEntity
<div>
    Entity: 
    <strong>@Model.Name</strong>
    @Html.ActionLink("Show All","Edit","Entity", new { id= @Model.ID}, null)
    @Html.LabelFor(m=>m.Icon):
    @Model.Icon
    @Html.LabelFor(m=>m.TypeName):
    @Model.TypeName
</div>

修改 路由配置:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

然而,ActionLink并不像我期望的那样工作。我希望ActionLink指向EntityController Edit操作,其中包含每个实体提供的ID。相反,它呈现以下网址:

http://localhost/MySite/Reports?action=Edit&controller=Entity&id=1

我不知道/ Reports /来自哪里。

谢谢!

1 个答案:

答案 0 :(得分:0)

使用Html.RouteLink指定路线名称

@Html.RouteLink("Show All", "Default", new { Controller = "Entity", Action = "Edit", id = Model.Id })

其他选项是更改地图路线顺序,默认路线应位于路线配置文件的末尾

此致