asp.net-mvc 从局部视图访问父模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15322180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access Parent Model from partial view
提问by AnimaSola
I'm asking because the partial view I will create is blank, with the purpose of creating a new child entity. I just need a quick, regardless if dirty, way to access the Parent Model from within the partial view. I need the Id of the parent.
我问是因为我将创建的部分视图是空白的,目的是创建一个新的子实体。我只需要一种快速的方法,无论是否脏,都可以从局部视图中访问父模型。我需要父母的身。
Does a partial view automatically have access to the model of the parent?
局部视图是否可以自动访问父模型?
回答by Darin Dimitrov
You cannot access the parent model from a partial view unless you pass some value to this partial as parameters when rendering it. For example in your main view:
您无法从局部视图访问父模型,除非您在渲染它时将一些值作为参数传递给该局部模型。例如在您的主视图中:
@model MyViewModel
...
@Html.Partial("_myPartial", new ViewDataDictionary(new { id = Model.Id }));
and then inside your partial you could access the Id:
然后在您的部分中,您可以访问Id:
<div>@ViewBag.Id</div>
Of course this is a pretty lousy way of passing data to a partial view. The correct way is to use a strongly typed view model.
当然,这是将数据传递给局部视图的一种非常糟糕的方式。正确的方法是使用强类型视图模型。
回答by Yushatak
I know this is an old topic, but I figured I'd just add my solution to the same problem anyway. I think it's a bit cleaner.
我知道这是一个老话题,但我想无论如何我都会将我的解决方案添加到相同的问题中。我认为它更干净一些。
Basically add a model to the partial view.
基本上将模型添加到局部视图。
The encapsulating view:
封装视图:
@model whatever
...
@Html.Partial("partialview", anotherwhatever)
The partial view:
部分观点:
@model anotherwhatever
<div>@Model.something</div>
...
In my case I just needed to pass a string into the partial view (just using it to shorten and partition code), so this was much more elegant than the other solution.
在我的情况下,我只需要将一个字符串传递到局部视图中(仅使用它来缩短和分区代码),因此这比其他解决方案更优雅。
I tried the other solution first and actually couldn't get it to work, it just acted as though the value I passed was blank.
我首先尝试了另一个解决方案,但实际上无法使其工作,它只是表现得好像我传递的值是空白的。
回答by JOpuckman
This ended up working for me.
这最终对我有用。
@model MyViewModel
...
@Html.Partial("_myPartial", new ViewDataDictionary { { "id", Model.Id } })
And inside the partial view, used this...
在局部视图中,使用了这个......
<div>@ViewBag.id</div>
回答by Stephen O
Under normal circumstances you can and should just pass the value down into the partial, but we ran into an edge case where this didn't make sense. If you're absolutely sure that you have to, you can easily access the parent Model or ViewBag as follows:
在正常情况下,您可以而且应该将值向下传递到部分,但我们遇到了一个没有意义的边缘情况。如果您绝对确定必须这样做,您可以轻松访问父模型或 ViewBag,如下所示:
// Get the parent's model
var model ViewContext.Controller.ViewData.Model as MyType
// Get the parent's ViewBag
var value = ViewContext.Controller.ViewBag.MyVariableName as MyType
The situation we ran into had the following constraints that lead us to making use of this:
我们遇到的情况有以下限制,导致我们使用它:
- Partials are automatically generated, inserted and rendered rather than explicitly called from our Razor Pages (In our case, these are ads inserted into an article at certain word counts by our CMS)
- The models we pass into the Partial when they're being automatically rendered are generated based on values saved in a DB, so we only want to have things which definitely won't change in the model (And can't include User-specific information or information which could change without the page being regenerated)
- 部分是自动生成、插入和呈现的,而不是从我们的 Razor 页面中明确调用(在我们的例子中,这些是我们的 CMS 以特定字数插入到文章中的广告)
- 我们在自动渲染时传递给 Partial 的模型是根据保存在数据库中的值生成的,所以我们只希望模型中绝对不会改变的东西(并且不能包含用户特定的信息或无需重新生成页面即可更改的信息)
回答by RC Cola
You can just pass the model IE:
您可以只传递模型 IE:
The encapsulating view:
封装视图:
@model MyModel
@Html.Partial("_myPartial", Model)
Partial View:
部分视图:
@model MyModel
Id = @Model.Id;

