C# MVC 5 Html.ActionLInk
C# MVC 5 Html.ActionLInk
我正在使用 Udemy 上的 C# MVC 5 应用程序,但我一直坚持使用 Html.ActionLink 从视图调用方法。我试过传递客户对象,然后决定尝试传递 id。
由于我 know/can 不明白的原因,这会引发 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>
}
当我单击来自 table 的链接时,出现了 HTTP 404 错误。非常感谢您的宝贵时间。
我认为正确的 URI 应该是 (/Customer/CustomerView/2) 而不是 (/CustomerController/CustomerView/2)。
下面是正确的代码行。
@Html.ActionLink(customer.Name, "CustomerView", "Customer", new { id = customer.Id }, null)
请从
移除控制器
<tr><td>@Html.ActionLink(customer.Name, "CustomerView", "CustomerController", new { id = customer.Id }, null)</td></tr>
试试这个 ActionLink 喜欢..
<tr><td>@Html.ActionLink(customer.Name, "CustomerView", "Customer", new { id = customer.Id }, null)</td></tr>
一定是工作..
我正在使用 Udemy 上的 C# MVC 5 应用程序,但我一直坚持使用 Html.ActionLink 从视图调用方法。我试过传递客户对象,然后决定尝试传递 id。
由于我 know/can 不明白的原因,这会引发 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>
}
当我单击来自 table 的链接时,出现了 HTTP 404 错误。非常感谢您的宝贵时间。
我认为正确的 URI 应该是 (/Customer/CustomerView/2) 而不是 (/CustomerController/CustomerView/2)。
下面是正确的代码行。
@Html.ActionLink(customer.Name, "CustomerView", "Customer", new { id = customer.Id }, null)
请从
移除控制器<tr><td>@Html.ActionLink(customer.Name, "CustomerView", "CustomerController", new { id = customer.Id }, null)</td></tr>
试试这个 ActionLink 喜欢..
<tr><td>@Html.ActionLink(customer.Name, "CustomerView", "Customer", new { id = customer.Id }, null)</td></tr>
一定是工作..