简体   繁体   English

渲染局部视图时,对象引用未设置为实例

[英]Object reference not set to an instance when render partial view

getting object reference exception when i try to render a partial view of different controller. 当我尝试渲染不同控制器的局部视图时,获取对象引用异常。

main view 主要观点

@model Models.Users

--------
@Html.Partial("~/Views/Address/AddressCreate.cshtml",Model.address)
--------

this is action 这是行动

   public ActionResult Create()
    {
        var model = new Users();
        return  View(model);
    }

partial view 部分视图

@model Models.Address
<div>@Html.TextBoxFor(m=>m.AddressLine1)</div>

partial view 部分视图

  public ActionResult AddressCreate()
    {
        var model = new Address();

        return PartialView(model);
    }

User and address class 用户和地址类别

public class Users
{
 public Address address {get; set;}

 }
 public class Address
  {
      // address objects
  }

It appears that the address property on your Users model is null when you passed it to the main view. 当您将其传递到主视图时, Users模型上的address属性似乎为空。

So make sure that this isn't the case by explicitly initializing it: 因此,请通过明确初始化来确保不是这种情况:

public ActionResult Create()
{
    var model = new Users();
    model.address = new Address();
    return View(model);
}

Or alternatively you could do that in your model's constructor: 或者,您可以在模型的构造函数中执行此操作:

public class Users
{
    public Users()
    {
        this.address = new Address();
    }

    public Address address { get; set; }
}

This being said, there's also some strange thing I've noticed in your code. 话虽如此,我在您的代码中也注意到了一些奇怪的事情。 You seem to have declared some AddressCreate controller action which is never used because you rendered the partial with the Html.Partial helper. 您似乎已经声明了一些AddressCreate控制器操作,该操作从未使用过,因为您使用Html.Partial帮助器渲染了部分Html.Partial If you wanted to render the partial using a child action then you should definitely use the Html.Action helper instead: 如果您想使用child action来渲染部分图片,那么您绝对应该使用Html.Action帮助器代替:

@model Models.Users

--------
@Html.Action("AddressCreate")
--------

Now the AddressCreate action will be invoked and successfully populate the Address model to the corresponding partial. 现在,将调用AddressCreate动作,并成功将Address模型填充到相应的部分。 In this case you no longer need to explicitly instantiate the address property on your main view model because it will never be used. 在这种情况下,您将不再需要在主视图模型上显式实例化address属性,因为它将永远不会使用。

Just make sure you understand the differences between Html.Partial and Html.Action helpers. 只要确保您了解Html.PartialHtml.Action帮助器之间的区别Html.Partial

As it is, the code will not be calling AddressCreate and hence never setting up the Address view model. 照原样,该代码将不会调用AddressCreate,因此永远不会设置Address视图模型。
Furthermore, your Create method does not populate an Address into the User it returns. 此外,您的Create方法不会将地址填充到它返回的用户中。

You need to either 1)Populate the User's address in the create method (and get rid of the AddressCreate method). 您需要1)在create方法中填充用户的地址(并摆脱AddressCreate方法)。

Or 要么

2)Use Html.Action instead of Html.Partial in your view, to call your AddressCreate method. 2)在视图中使用Html.Action而不是Html.Partial来调用AddressCreate方法。

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

相关问题 渲染我的部分视图时,它没有设置为 object 的实例 - When rendering my partial view it is not set it an instance of an object MVC4部分视图错误“对象引用未设置为对象的实例。” - MVC4 Partial View Error “Object reference not set to an instance of an object.” 部分视图错误未将对象引用设置为对象的实例。 MVC4剃刀 - Partial view error Object reference not set to an instance of an object. MVC4 Razor 加载数据时,对象引用未设置为Html.Partial MVC中的对象实例 - Object reference not set to an instance of an object in Html.Partial MVC when load data MVC5 对象引用未设置为 Scripts.Render 上的对象实例 - MVC5 Object reference not set to an instance of an object on Scripts.Render 转换时未将对象引用设置为对象的实例 - Object reference not set to an instance of an object when converting 未设置Partial View错误对象引用不理解原因 - Partial View error object reference not set don't understand why 添加WCF服务引用时,“对象引用未设置为对象的实例” - “object reference not set to an instance of an object” when adding a WCF service reference 你调用的对象是空的。 如何查看堆栈 - Object reference not set to an instance of an object. How to view the stack 对象引用未设置为实例 - object reference not set to an instance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM