[英]Linking PartialView to View returns System.Web.Mvc.WebViewPage<TModel>.Model.get returned null
I looked up everywhere and tried multiple solutions but none of them would work. 我到处查找并尝试了多种解决方案,但没有一个可行。 In my MVC website, I have a cart section and a checkout section.
在我的MVC网站上,我有一个购物车部分和一个结帐部分。 I want to make it so inside my checkout section, i have a small cart section to show the cart.
我想在结帐部分中做到这一点,我有一个小的购物车部分来显示购物车。
This is my partial view _CartItems.cshtml 这是我的局部视图_CartItems.cshtml
@model IEnumerable<ArrowDefenseSystems.Models.Cart>
@{
ViewBag.Title = "Your Cart";
}
@if (Model != null)
{
foreach (var item in Model)
{
<div class="itemInfo row">
<img class="col-md-3" src="@Url.Content("~/Content/" + @Html.DisplayFor(modelItem => item.productImage))" height="100px">
<div class="CartItemText col-md-9">
<h3>@Html.DisplayFor(modelItem => item.productName)</h3>
<i>$@Html.DisplayFor(modelItem => item.productPrice)</i><br>
Quantity : @Html.DisplayFor(modelItem => item.quantityChosen)<br>
</div>
</div>
<hr />
}
}
When I launch the Partial View by itself, it shows the items fine and everything runs correctly. 当我单独启动Partial View时,它可以很好地显示项目,并且一切运行正常。
This is how I'm linking the partial view to the view Checkout.cshtml 这就是我将部分视图链接到视图Checkout.cshtml的方式
@model ArrowDefenseSystems.Models.ParentViewModel
...
...
@Html.Partial("_CartItems", Model.Cart)
When I run this code i get the following error on the code above: 当我运行此代码时,我在上面的代码中收到以下错误:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.Web.Mvc.WebViewPage<TModel>.Model.get returned null.
ParentViewModel: ParentViewModel:
public class ParentViewModel
{
public Checkout Checkout { get; set; }
public Cart Cart { get; set; }
}
Checkout Controller (theres more but its unnecessary): Checkout控制器(更多功能,但不必要):
[HttpGet]
public ActionResult _CartItems()
{
return PartialView(db.Carts.ToList());
}
public ActionResult Checkout()
{
return View();
}
I've tried many solutions but all return the same error. 我已经尝试了许多解决方案,但是都返回相同的错误。 What am I missing?
我想念什么?
I think there is a fundamental misunderstanding here: @Html.Partial("_CartItems", Model.Cart)
will not call your _CartItems()
action. 我认为这里有一个基本的误解:
@Html.Partial("_CartItems", Model.Cart)
将不会调用您的_CartItems()
动作。 Instead, it will create a new instance of the _CartItems.cshtml
partial view, and use your Model.Cart
parameter as it's Model
. 相反,它将创建
_CartItems.cshtml
部分视图的新实例,并将Model.Cart
参数用作Model
。
In this case, there are two problems: 在这种情况下,有两个问题:
_CartItems.cshtml
is expecting a model with a type of IEnumerable<ArrowDefenseSystems.Models.Cart>
, whereas you are passing it a model with a type of Cart
(via the Model.Cart
parameter) _CartItems.cshtml
期望使用类型为IEnumerable<ArrowDefenseSystems.Models.Cart>
的模型,而您正在将其传递为具有Cart
类型的模型(通过Model.Cart
参数) Model.Cart
is not being instantiated in your Checkout
action Checkout
操作中实例化Model.Cart
So, to fix this, you should first change your ParentViewModel
to be: 因此,要解决此问题,您首先应该将
ParentViewModel
更改为:
public class ParentViewModel
{
public Checkout Checkout { get; set; }
public IEnumerable<Cart> Carts { get; set; }
}
Then, update your Checkout
action to be: 然后,将您的
Checkout
操作更新为:
public ActionResult Checkout()
{
var viewModel = new ParentViewModel
{
Carts = db.Carts.ToList()
}
return View(viewModel);
}
And change your usage of Model.Cart
to Model.Carts
(due to the rename above) 并将您对
Model.Cart
的使用Model.Cart
为Model.Carts
(由于上述重命名)
After that, you can remove your _CartItems
action as it is no longer used. 之后,您可以删除
_CartItems
操作,因为它不再使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.