【发布时间】:2019-05-10 07:04:27
【问题描述】:
我在视图中有一张桌子,上面有一些汽车的详细信息。单击“租用”按钮后,将我重定向到另一个视图并在那里显示所选行的数据。使用代码上已有的按钮,我只能在 URL 中显示该行的详细信息,但它们不会显示在页面上。
这是我的 ListOfCars 视图:
@model IEnumerable<CarDataAccess.Car>
@{
ViewBag.Title = "List";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>ListCars</title>
<script src="~/Scripts/OnClick.js">
</script>
</head>
<body>
@*<p>
@Html.ActionLink("Create New", "Post")
</p>*@
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.model)
</th>
<th>
@Html.DisplayNameFor(model => model.make)
</th>
<th>
@Html.DisplayNameFor(model => model.location)
</th>
<th>
@Html.DisplayNameFor(model => model.avaStart)
</th>
<th>
@Html.DisplayNameFor(model => model.avaEnd)
</th>
<th>
@Html.DisplayNameFor(model => model.price)
</th>
<th>
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.model)
</td>
<td>
@Html.DisplayFor(modelItem => item.make)
</td>
<td>
@Html.DisplayFor(modelItem => item.location)
</td>
<td>
@Html.DisplayFor(modelItem => item.avaStart)
</td>
<td>
@Html.DisplayFor(modelItem => item.avaEnd)
</td>
<td>
@Html.DisplayFor(modelItem => item.price)
</td>
<td>
<td>@Html.ActionLink("Rent", "Payment", new { id = item.Id })</td>
</td>
</tr>
}
</table>
<div>
<a href="http://localhost:6664/User/Login">Login</a>
</div>
这是我希望显示该行的另一个视图:
@model IEnumerable<CarDataAccess.Car>
@{
/**/
ViewBag.Title = "Payment";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Payment</title>
</head>
<body>
<table class="paymenttable">
</table>
</body>
</html>
控制器代码:
namespace RentCarApplication.Controllers
{
public class HomeController : Controller
{
BookCarDBEntities db = new BookCarDBEntities();
public ActionResult Index(int? id)
{
ViewBag.Title = "Home Page";
return View();
}
public ActionResult ListCars()
{
string username = User.Identity.Name;
var cars = db.Cars.ToList();
return View(cars);
}
public ActionResult Payment(int id)
{
using (BookCarDBEntities entities = new BookCarDBEntities())
{
var entity = entities.Cars.FirstOrDefault(c => c.Id == id);
if (entities != null)
{
return View(entity);
}
else
{
return View("Not Found");
}
}
}
}
}
【问题讨论】:
-
控制器长什么样?您是否显示了 Rent Car 视图的所有代码?你传递那个视图的模型是什么?视图需要 IEnumerable
。那个视图不会是一个单一的汽车模型。 -
@Haldo 我刚刚添加了控制器,您可以查看一下。我不知道我应该写什么而不是 IEnumerable
?
标签: html model-view-controller view row display