【问题标题】:C# MVC 5 Html.ActionLInkC# MVC 5 Html.ActionLInk
【发布时间】:2017-05-10 20:25:13
【问题描述】:

我正在使用 Udemy 上的 C# MVC 5 应用程序,但我一直坚持使用 Html.ActionLink 从视图调用方法。我试过传递客户对象,然后决定尝试传递 id。

由于我不知道/无法弄清楚的原因,这会引发 http 404 错误,同时显示正确的 Url (/CustomerController/CustomerView/2)。这是我的代码:

RouteConfig.cs

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

namespace Vidly
{
public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapMvcAttributeRoutes();

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

CustomerController.cs

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

namespace Vidly.Controllers
{
public class CustomerController : Controller
{

    private List<CustomerModels> customers = new List<CustomerModels>
        {
            new CustomerModels {Id = 0, Name = "Theo Greer" },
            new CustomerModels {Id = 1, Name = "Mark Pate" },
            new CustomerModels {Id = 2, Name = "Jerry Jones" },
            new CustomerModels {Id = 3, Name = "Mary Alexander" },
            new CustomerModels {Id = 4, Name = "Patricia Smith" }
        };

    // GET: Customer
    public ActionResult Index()
    {
        return View(customers);
    }

    public ActionResult CustomerView(int id)
    {
        CustomerModels tempCust = customers.FirstOrDefault(CustomerModels => CustomerModels.Id == id);
        return View(tempCust);
    }
}
}

Index.cshtml

@model List<Vidly.Models.CustomerModels>
@{ }
<h2>Customers</h2>

<table class="table table-bordered table-hover">
<tr>
    <th>Customer</th>
</tr>
@foreach (var customer in Model)
{
    <tr><td>@Html.ActionLink(customer.Name, "CustomerView", "CustomerController", new { id = customer.Id }, null)</td></tr>
}

当我单击表中的链接时,会引发 http 404 错误。非常感谢您的宝贵时间。

【问题讨论】:

    标签: model-view-controller web-applications asp.net-mvc-5 asp.net-routing html.actionlink


    【解决方案1】:

    我认为正确的 URI 应该是 (/Customer/CustomerView/2) 而不是 (/CustomerController/CustomerView/2)。

    下面是正确的代码行。

    @Html.ActionLink(customer.Name, "CustomerView", "Customer", new { id = customer.Id }, null)

    【讨论】:

    • 非常感谢。这让它工作了,同时将我的视图名称更改为 CustomerView。
    【解决方案2】:

    请移除控制器

    <tr><td>@Html.ActionLink(customer.Name, "CustomerView", "CustomerController", new { id = customer.Id }, null)</td></tr>
    

    试试这个 ActionLink Like..

    <tr><td>@Html.ActionLink(customer.Name, "CustomerView", "Customer", new { id = customer.Id }, null)</td></tr>
    

    它一定是工作..

    【讨论】:

      猜你喜欢
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      • 2012-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-09
      相关资源
      最近更新 更多