使用可用于查看的对象,不适用于部分视图 [英] using object available to view, unavailable to partial view

查看:89
本文介绍了使用可用于查看的对象,不适用于部分视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个观点和一个局部观点.在控制器操作方法中,我有一个using语句,用于建立一些后端线程上下文,包括提供对模型对象的访问以在视图中显示.

I have a view and a partial view. Within the controller action methods, I have a using statement which establishes some back-end threading context, including providing access to a model object for display in the view.

我遇到的问题是,尽管上下文和关联的模型对象可用于主视图,但它们在呈现部分视图之前就已放置.无论我是将模型对象从视图传递到局部视图,还是以其他方式引用它,似乎都是这种情况.

The problem that I'm having is that while the context and the associated model object are available for the main view, they are disposed before the partial view is rendered. This seems to be the case regardless of whether I pass the model object from the view to the partial view, or reference it some other way.

控制器视图操作方法:

public ActionResult Index()
{
  using (var context = GetContext()) {
    return View(Context.MyModelObject);
  }
}

控制器局部视图操作方法:

[ChildActionOnly]
public ActionResult MyPartialView(ModelObject modelObject)
{
  return PartialView(modelObject);
}

视图:

@model ModelObject

@Model.Name

@{Html.RenderAction("MyPartialView", "MyController", new {modelObject = @Model});}

局部视图:

@model ModelObject

@foreach(var item in Model.Items) {
  <div>@item.Name</div>
}

如果我注释掉视图的RenderAction行,则该视图显示得很好,可以访问模型的属性,因为在使用结束时尚未放置上下文.

If I comment out the RenderAction line of the view, the view displays just fine, accessing the property of the model because the context has not yet been disposed at the end of the using.

但是,如果我出于呈现部分视图的目的而将其保留,则上下文已被丢弃,从而使我的模型对象不再可访问.我认为主视图控制器方法中的使用要等到该视图及其所有部分视图都呈现后才能完成,但事实并非如此.是否将部分视图推迟到其他时间渲染?有办法解决吗?

However, if I leave it in with the intent of the partial view being rendered, the context has been disposed of, leaving my model object no longer accessible. I would think the using in the main view controller method would not be finished until the view and all its partial views are rendered, but that doesn't seem to be the case. Are partial views deferred to be rendered at some other time? Is there a way around this?

现在,我有一个解决方案,其中涉及编写许多额外的代码:制作视图模型"对象,该对象仅具有我需要在此特定视图中显示的模型对象的位,而尚未显示放置,然后让视图和局部视图使用该视图.但是,这将是非常不希望的,因为这将导致模型和视图模型之间有很多重复,这将是一个子集.直接在视图中使用模型将非常方便.

Right now I have a solution which will involve writing a lot of extra code: making a "view model" object which has only the bits of the model object that I need for display in this specific view while it hasn't yet been disposed, then having the view and partial views use that. However, this will be very undesirable as it will result in a lot of duplication between the model and the view models, which will be a subset. It would be very convenient to use the model directly in the view.

另一种可能性是,我们可以改变后端以不同的方式处理事物,以便它们在进行渲染时仍可用于部分视图,但这似乎也很容易.

The other possibility is that we could alter our backend to dispose of things differently such that they are still available for the partial view whenever it gets around to rendering, but this also seems like a hack.

总而言之,在视图/控制器中是否有更好的方法来做到这一点?我是在做一些错误的假设吗?

In summary, is there a better way to do this in the view/controller? Am I making some erroneous assumptions?

推荐答案

MVC在设计时考虑了单元可测试性.这样,当您调用View()时,它不会渲染视图,它只是创建一个从控制器返回后将被渲染的结构.

MVC was designed with Unit testability in mind. As such, it doesn't render the view when you call View(), it just creates a structure which gets rendered after it returns from the controller.

此设计使对控制器进行单元测试更加容易,因为它实际上不需要活动的Web服务器连接即可执行控制器逻辑.

This design makes it easier to unit test controllers because it doesn't actually require an active web server connection to execute the controller logic.

但是,这也意味着,由于在控制器中有一个using语句,因此直到从函数返回后才实际渲染视图,因此using在数据上下文上调用了delete.因此,您的查询将不会执行.

But, this also means that since you have a using statement in your controller, the view is not actually rendered until after you have returned from the function, and thus the using has called delete on the data context. Thus, your queries will not execute.

您可以通过在控制器内执行查询来解决此问题,例如在对象上调用.ToList().您仍然必须小心,不要在返回后执行任何惰性操作.

You get around this by executing the query within the controller, such as calling .ToList() on the object. You still have to be careful not to execute any lazy operations after returning.

但是,更好的方法是不要将数据对象直接传递到视图,而应使用从返回的数据对象中填充的视图模型.

However, a better approach is to not pass your data objects directly to the view, but instead use a view model which you populate from the returned data objects.

这篇关于使用可用于查看的对象,不适用于部分视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆