dav_i
dav_i

Reputation: 28097

Passing data from Partial View to its parent View

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.

Upvotes: 8

Views: 29763

Answers (5)

mcorte
mcorte

Reputation: 52

The simpler thing you could do is in _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

Upvotes: 1

Hitanshi Mehta
Hitanshi Mehta

Reputation: 19

You can simply use javascript for this. I have this hidden textbox in partial view and i want to access it's value in parent view

<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 ;

Upvotes: -1

dav_i
dav_i

Reputation: 28097

So after some thought I came up with this:

View.cshtml:

@{ 
    dynamic properties = new NullingExpandoObject();

    var result = Html.Partial("_PartialView", (NullingExpandoObject)properties); 
}

<div id="@properties.Id">
    @result
<div>

_PartialView.cshtml:

@{ Model.Id = "foo"; }

<div>
    Content
</div>

Where NullingExpandoObject is Jon Skeet's nullable dynamic dictionary

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You could share state between views using the 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 BEFORE invoking the partial view which obviously is impossible.

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.

Upvotes: 25

Haritha
Haritha

Reputation: 1508

I have a suggestion for you.

Put hidden input fields in the partial view and get them from javascript.

Ex: In _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.

Upvotes: 3

Related Questions