C# 将数据从局部视图传递到其父视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19766502/
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
Passing data from Partial View to its parent View
提问by dav_i
If I have a View and a Partial View, is there any way that I can pass data from the Partial View to the parent?
如果我有一个视图和一个局部视图,有什么方法可以将数据从局部视图传递给父视图?
So if I have View.cshtml
:
所以如果我有View.cshtml
:
<div id="@someDataFromPartialSomehow">
@Html.Partial("_PartialView")
</div>
And _PartialView.cshtml
:
并且_PartialView.cshtml
:
@{ someDataFromPartialSomehow = "some-data" }
<div>
Some content
</div>
How would I go about implementing something like this?
我将如何实施这样的事情?
I tried to use ViewBag.SomeDataFromPartialSomehow
, but this just results in null
in the Parent.
我尝试使用ViewBag.SomeDataFromPartialSomehow
,但这只会导致null
父级。
An attempt
一次尝试
To try get around the problem of data being generated before being called I tried this:
为了解决在被调用之前生成数据的问题,我尝试了这个:
View.cshtml
:
View.cshtml
:
@{ var result = Html.Partial("_PartialView"); }
<div id="@ViewData["Stuff"]">
@result
<div>
_PartialView.cshtml
:
_PartialView.cshtml
:
@{ ViewData["Stuff"] = "foo"; }
<div>
Content
</div>
But the call to @ViewDate["Stuff"]
still renders nothing unfortunately.
但@ViewDate["Stuff"]
不幸的是,调用仍然没有任何结果。
采纳答案by Darin Dimitrov
You could share state between views using the HttpContext.
您可以使用 HttpContext 在视图之间共享状态。
@{
this.ViewContext.HttpContext.Items["Stuff"] = "some-data";
}
and then:
进而:
@{ var result = Html.Partial("_PartialView"); }
<div id="@this.ViewContext.HttpContext.Items["Stuff"]">
@result
<div>
Except that the example you have shown in your question:
除了您在问题中显示的示例:
<div id="@someDataFromPartialSomehow">
@Html.Partial("_PartialView")
</div>
you are attempting to use the someDataFromPartialSomehow
even BEFOREinvoking the partial view which obviously is impossible.
您正在尝试使用someDataFromPartialSomehow
even BEFORE调用部分视图,这显然是不可能的。
Also bear in mind that what you are trying to achieve is bad design. If a partial view can only work in the context of some specific parent, then you might need to rethink your separation of views. Partial views is something that must be INDEPENDENT and REUSABLE, no matter in which context it is being placed. If it assumes things about the hosting parent then there's a serious design problem here.
还要记住,您试图实现的是糟糕的设计。如果局部视图只能在某些特定父级的上下文中工作,那么您可能需要重新考虑视图的分离。部分视图必须是独立的和可重用的,无论它被放置在哪个上下文中。如果它假设有关托管父母的事情,那么这里存在严重的设计问题。
回答by Haritha
I have a suggestion for you.
我有一个建议给你。
Put hidden input fields in the partial view and get them from javascript.
将隐藏的输入字段放在局部视图中并从 javascript 中获取它们。
Ex: In _PartialView.cshtml
例如:在_PartialView.cshtml
<input type="hidden" id="someDataFromPartialSomehow" value="5" />
In your view
在你看来
<script>
$(document).ready(function(){
var someDataFromPartialSomehow = $("#someDataFromPartialSomehow").val();
});
</script>
Note that you have to write the js function inside the document ready function because the partial view should be fully loaded.
请注意,您必须在文档就绪函数中编写 js 函数,因为应该完全加载局部视图。
回答by dav_i
So after some thought I came up with this:
所以经过一些思考,我想出了这个:
View.cshtml
:
View.cshtml
:
@{
dynamic properties = new NullingExpandoObject();
var result = Html.Partial("_PartialView", (NullingExpandoObject)properties);
}
<div id="@properties.Id">
@result
<div>
_PartialView.cshtml
:
_PartialView.cshtml
:
@{ Model.Id = "foo"; }
<div>
Content
</div>
Where NullingExpandoObject
is Jon Skeet's nullable dynamic dictionary
回答by Hitanshi Mehta
You can simply use javascriptfor this. I have this hidden textbox in partial view and i want to access it's value in parent view
为此,您可以简单地使用javascript。我在局部视图中有这个隐藏的文本框,我想在父视图中访问它的值
<input type="text" name="allexamcount" id="allexamcount" value="@TempData["allexamcnt"]" hidden />
So, simplest way is as follow:
所以,最简单的方法如下:
var allexamcount = document.getElementById("allexamcount").value;
document.getElementById("examcount").value = allexamcount ;
回答by mcorte
The simpler thing you could do is in _PartialView.cshtml:
您可以做的更简单的事情是在 _PartialView.cshtml 中:
@model dynamic
@{
Model.Stuff = "stuff things";
}
and in parent view:
在父视图中:
@{
Html.RenderPartial("_PartialView", (object) ViewBag);
}
then in parent view you can use:
然后在父视图中您可以使用:
@ViewBag.Stuff