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
:
<div id="@someDataFromPartialSomehow">
@Html.Partial("_PartialView")
</div>
And _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.
An attempt
To try get around the problem of data being generated before being called I tried this:
View.cshtml
:
@{ var result = Html.Partial("_PartialView"); }
<div id="@ViewData["Stuff"]">
@result
<div>
_PartialView.cshtml
:
@{ ViewData["Stuff"] = "foo"; }
<div>
Content
</div>
But the call to @ViewDate["Stuff"]
still renders nothing unfortunately.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…