可能是一个重复的问题和一个很长的问题。但我没有得到我的解决方案,问题很容易专家。
在我的HomeController
中有三个名为Index()
,Customer()
和Item()
的ActionResult()。
模特课程:
namespace Customer.Models
{
public class CustomerInfo
{
public int CustomerId { get; set; }
public string CustomerName { get; set; }
public string ItemName { get; set; }
}
}
我在谈论我的Customer()
。在Customer()
中,列表将返回CustomerView
页面。
HomeController中:
List<CustomerInfo> customers = new List<CustomerInfo>();
public ActionResult Customer()
{
customers.Add(new CustomerInfo { CustomerName= "Nafeeur"});
customers.Add(new CustomerInfo { CustomerName = "Rasel" });
customers.Add(new CustomerInfo { CustomerName = "Fagun" });
return View(customers);
}
CustomerView
页面:
@model IEnumerable<Customer.Models.CustomerInfo>
@using Customer.Models;
@{
ViewBag.Title = "Customers";
}
<h2>@ViewBag.Title.</h2>
<h1>Customer Name</h1>
<ul>
@foreach(CustomerInfo customer in @Model)
{
<li>
@Html.ActionLink(customer.CustomerName, "Index", "Customer", new {id = customer.CustomerId})
</li>
}
</ul>
在此View页面中,我将id传递给名为CustomerController
代码:
namespace Customer.Controllers
{
public class CustomerController : Controller
{
// GET: Customer
public ActionResult Index(int id)
{
var list = new List<CustomerInfo>();
var item = list.Find(x => x.GetId(id));
return View(item);
}
}
}
查看CustomerController
视图的页面:
@model Customer.Models.CustomerInfo
@{
ViewBag.Title = "Customer Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Customer Index</h2>
<h3>@Model.CustomerId</h3>
<h3>@Model.CustomerName</h3>
<h3>@Model.ItemName</h3>
我的问题是,当我点击任何客户时,它无法正常工作。
答案 0 :(得分:1)
在我看来(至少你得到错误的原因)是你只需要在CustomerInfo类中实现GetId()。除非我完全关闭(我可能会),否则这不是某种你可以使用的隐式方法。你必须写它。例如,这样的东西可以进入你的CustomerInfo类。
public int GetId()
{
return this.CustomerId;
}
然而,就像Stephen在评论中指出的那样,你仍然需要为客户设置id。如果您没有使用数据库(因为它看起来不是您的评论)并且您只是在创建某种演示应用程序,那么您需要在代码中编写某种方法来设置每个数据库的ID。列表中的那些客户(即Nafeer,Rasel,Fagun)。
希望这会有所帮助。