我在View中有一张桌子,上面有一些Car的详细信息。单击“出租”按钮后,将我重定向到另一个视图并在其中显示所选行的数据。使用代码中已有的按钮,我只能在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");
}
}
}
}
}
答案 0 :(得分:0)
在控制器操作Payment(int id)
中,您将类型CarDataAccess.Car
的单个实体传递给视图(代码entities.Cars.FirstOrDefault(c => c.Id == id)
中的行从数据库中获取单个实体)。
Payment
视图需要一个IEnumerable<CarDataAccess.Car>
类型的集合。您可以在视图@model IEnumerable<CarDataAccess.Car>
的第一行看到它。将其更改为@model CarDataAccess.Car
,看看是否可以解决问题。
将视图更新为此:
@model 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>
@* access model properties here *@
<span>@Model.Id</span>
</body>
</html>
您的控制器中也存在错误。检查Car
实体是否不是null,而不是数据库上下文(entities
):
public ActionResult Payment(int id)
{
using (BookCarDBEntities entities = new BookCarDBEntities())
{
var entity = entities.Cars.FirstOrDefault(c => c.Id == id);
if (entity != null) // check if entity is null, not entities
{
return View(entity);
}
else
{
return View("Not Found");
}
}
}
控制器中也存在另一个问题-您已声明数据库上下文(BookCarDBEntities
)的多个实例。最好像在Payment方法中那样,将数据库上下文包装在using
语句中。对ListCars
方法执行相同操作,然后从控制器顶部删除BookCarDBEntities db = new BookCarDBEntities();
。如果将上下文包装在using语句中,则可以确保垃圾收集器正确处理和清除了上下文。
ListCars方法应类似于:
public ActionResult ListCars()
{
string username = User.Identity.Name;
var cars = new List<CarDataAccess.Car>();
using (var db = new BookCarDBEntities())
{
cars = db.Cars.ToList();
}
return View(cars);
}