简体   繁体   English

将PartialView链接到View返回System.Web.Mvc.WebViewPage <TModel> .Model.get返回null

[英]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.CartModel.Carts (由于上述重命名)

After that, you can remove your _CartItems action as it is no longer used. 之后,您可以删除_CartItems操作,因为它不再使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Scaffolded create View返回System.Web.Mvc.WebViewPage <TModel> .Model.get返回null。 发布之前 - Scaffolded create View returns System.Web.Mvc.WebViewPage<TModel>.Model.get returned null. before post System.Web.Mvc.WebViewPage<T> .Model.get 返回 null - System.Web.Mvc.WebViewPage<T>.Model.get returned null System.NullReferenceException:System.Web.Mvc.WebViewPage<t> .Model.get 返回 null</t> - System.NullReferenceException: System.Web.Mvc.WebViewPage<T>.Model.get returned null &#39;Model&#39;与声明&#39;System.Web.Mvc.WebViewPage冲突 <TModel> 。模型 - 'Model' conflicts with the declaration 'System.Web.Mvc.WebViewPage<TModel>.Model ASP.NET MVC NullreferenceException:System.Web.Mvc.WebViewPage <TModel> 。模型.get。 <Field at offset 0x00000004> 为空 - ASP.NET MVC Nullreferenceexception: System.Web.Mvc.WebViewPage<TModel>.Model.get.<Field at offset 0x00000004> was null MVC3 CS0135:“模型”与声明“ System.Web.Mvc.WebViewPage”冲突 <TModel> 。模型&#39; - MVC3 CS0135: 'Model' conflicts with the declaration 'System.Web.Mvc.WebViewPage<TModel>.Model' MVC:foreach循环提供System.Web.Mvc.WebViewPage <TModel> 。型号错误 - MVC : foreach loop gives System.Web.Mvc.WebViewPage<TModel>.Model error 怎么修 ” <TModel> .Model.get返回null”在MVC应用中? - How to fix “<TModel>.Model.get returned null” in MVC app? Mvc.WebViewPage <T> .Model.get在创建视图中返回null和ID - Mvc.WebViewPage<T>.Model.get returned null and Id in Create view Microsoft.AspNetCore.Mvc.Razor.RazorPage<tmodel> .Model.get 返回 null - 如何解决这个问题?</tmodel> - Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>.Model.get returned null - How to solve this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM