Model view controller C#MVC 5 Html.ActionLInk
我正在Udemy上运行一个C#mvc5应用程序,我一直在使用Html.ActionLink从视图调用一个方法。我已尝试传递customer对象,然后决定尝试传递id 出于我不知道/无法理解的原因,这是在显示正确的Url(/CustomerController/CustomerView/2)时引发http 404错误。这是我的密码: RouteConfig.csModel view controller C#MVC 5 Html.ActionLInk,model-view-controller,web-applications,asp.net-mvc-5,asp.net-routing,html.actionlink,Model View Controller,Web Applications,Asp.net Mvc 5,Asp.net Routing,Html.actionlink,我正在Udemy上运行一个C#mvc5应用程序,我一直在使用Html.ActionLink从视图调用一个方法。我已尝试传递customer对象,然后决定尝试传递id 出于我不知道/无法理解的原因,这是在显示正确的Url(/CustomerController/CustomerView/2)时引发http 404错误。这是我的密码: RouteConfig.cs using System; using System.Collections.Generic; using System.Linq; u
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 }
);
}
}
}
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);
}
}
}
CustomerController.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 }
);
}
}
}
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);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用Vidly.模型;
名称空间Vidly.Controllers
{
公共类CustomerController:控制器
{
私人名单客户=新名单
{
新客户模型{Id=0,Name=“Theo Greer”},
新客户模型{Id=1,Name=“Mark Pate”},
新客户模型{Id=2,Name=“Jerry Jones”},
新客户模型{Id=3,Name=“Mary Alexander”},
新客户模型{Id=4,Name=“Patricia Smith”}
};
//获取:客户
公共行动结果索引()
{
返回视图(客户);
}
公共操作结果CustomerView(int id)
{
CustomerModels tempCust=customers.FirstOrDefault(CustomerModels=>CustomerModels.Id==Id);
返回视图(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>
}
@型号列表
@{ }
客户
顾客
@foreach(模型中的var客户)
{
@ActionLink(customer.Name,“CustomerView”,“CustomerController”,new{id=customer.id},null)
}
当我单击表中的链接时,抛出一个HTTP404错误。非常感谢您抽出时间 我认为合适的URI应该是(/Customer/CustomerView/2),而不是(/CustomerController/CustomerView/2) 下面是正确的代码行
@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>
@Html.ActionLink(customer.Name,“CustomerView”,“CustomerController”,新的{id=customer.id},null)
试试这个ActionLink,比如
<tr><td>@Html.ActionLink(customer.Name, "CustomerView", "Customer", new { id = customer.Id }, null)</td></tr>
@Html.ActionLink(customer.Name,“CustomerView”,“customer”,new{id=customer.id},null)
一定是工作。非常感谢。这使它工作起来,同时将我的视图名称更改为CustomerView。