将一个视图的值显示到另一个视图 (MVC)

Displaying values of a view to another view (MVC)

我在视图中有一个 table,上面有一些汽车的详细信息。单击 'Rent' 按钮后,将我重定向到另一个视图并在那里显示所选行的数据。使用代码中已有的按钮,我只能在 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");
            }

        }






    }
}
}

在控制器操作 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 实体是否不为空,而不是数据库上下文 (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)。最好将数据库上下文包装在 using 语句中,就像您在 Payment 方法中所做的那样。对 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);
}