0

私はUdemyのC#MVC 5アプリケーションを使用しています.Html.ActionLinkを使用してビューからメソッドを呼び出すことに固執しました。私は顧客オブジェクトを渡そうとしましたが、IDを渡そうとしたときに解決しました。C#MVC 5 Html.ActionLInk

わからない/わからない理由のために、これは適切なURL(/ CustomerController/CustomerView/2)を表示しながらhttp 404エラーを投げています。ここに私のコードは次のとおりです。

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エラーがスローされます。あなたのお時間をありがとうございました。

答えて

0

適切なURIは(/ Customer/CustomerView/2)ではなく、/ コントローラ/CustomerView/2である必要があります。

以下は正しいコード行です。 Html.ActionLink(customer.Name、 "CustomerView"、 "顧客"、新しい{ID = customer.Id}、ヌル)

+0

おかげ束@

。それは私のビュー名をCustomerViewに変更すると同時にそれを働かせました。 –