任何人都可以针对所附图像中遇到的以下问题为我提供帮助。我正在尝试转到以下URL:http:// localhost:49849 / Customers,但我不能。在此先感谢...只是为了让您知道我正在跟踪莫什的课程。
这是型号代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations; //this is a namespace
namespace Vidly2.Models
{
public class Customers
{
public int id { get; set; }
[Required] // we cal changing the conventions dataannotations
[StringLength(255)]
public string name { get; set; }
public bool IsSubscribedToNewsletter { get; set; }
public MembershipType MembershipType { get; set; } // we call it navigation property because it allows us to navigate to another type
public byte MembershipTypeId { get; set; } //Fk
}
}
这是控制器代码:
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Vidly2.Models;
namespace Vidly2.Controllers
{
public class CustomersController : Controller
{
public ViewResult Index()
{
var customers = GetCustomers();
return View(customers);
}
public ActionResult Details(int id)
{
var customer = GetCustomers().SingleOrDefault(c => c.id == id);
if (customer == null)
return HttpNotFound();
return View(customer);
}
private IEnumerable<Customers> GetCustomers()
{
return new List<Customers>
{
new Customers { id =1, name = "John Smith" },
new Customers { id =2, name = "Mary Williams" }
};
}
}
}
这是查看代码,其中提到了有关编译项目的错误:
@using System.Web.Mvc.Html;
@model IEnumerable<Vidly2.Models.Customers>
@*
Note: I've set the model for this view to IEnumerable<Customer>.
This is a simple interface implemented by the list class. Since
in this view we only want to iterate over this list, and we don't
need any of the operations in the List class (eg Add, Remove, etc),
it's better to use the IEnumerable interface, which allows use to
iterate over the list. If in the future, we replace the List with a
different data structure, as long as it is enumerable, our view code
will remain unchanged.
*@
@{
ViewBag.Title = "Customers";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Customers</h2>
@if (!Model.Any())
{
<p>We don't have any customers yet.</p>
}
else
{
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Customer</th>
</tr>
</thead>
<tbody>
@foreach (var customer in Model)
{
<tr>
<td>@Html.ActionLink(customer.Name, "Details", "Customers", new { id = customer.id }, null)</td>
</tr>
}
</tbody>
</table>
}
答案 0 :(得分:0)
您有name
的属性不匹配,就像模型中的情况一样,name
在较小的情况下,但是鉴于您使用的customer.Name
应该是customer.name
>
@Html.ActionLink(customer.name, "Details", ...