[英]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.Partial
和Html.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.