Reputation: 21
May be it's a duplicate question and a long question. But I didn't get my solution and problem is easy to expert.
In my HomeController
there are three ActionResult() named Index()
, Customer()
and Item()
.
Model Class:
namespace Customer.Models
{
public class CustomerInfo
{
public int CustomerId { get; set; }
public string CustomerName { get; set; }
public string ItemName { get; set; }
}
}
I am talking about my Customer()
. In Customer()
a list will be return to CustomerView
page.
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
page:
@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>
From this View page I'm passing id to another Controller page named CustomerController
Code:
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);
}
}
}
View page of CustomerController
View:
@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>
My problem is, when I click on any customer it is not working.
Upvotes: 0
Views: 6900
Reputation: 3642
It seems to me (at least the reason you are getting the error) is that you just need to implement GetId() within you CustomerInfo class. Unless I'm totally off (and I might be) this is not some kind of implicit method that you can just use. You have to write it. For example something like this could go into your CustomerInfo class.
public int GetId()
{
return this.CustomerId;
}
However, like Stephen pointed out in a comment, you still have to actually set the id for the customer. If you aren't using a database (as it appears you are not from your comment) and you are just creating some kind of a demo app, you need to just write some kind of method in your code that sets the id for each of those customers in your list (i.e. Nafeer, Rasel, Fagun).
Hope this helps.
Upvotes: 1