Passing data from Partial View to its parent View

access partial view model from parent view
pass data from partial view to controller
collect form data from both main and partial view in mvc
mvc partial view update parent model
pass model to partial view using jquery
pass object to partial view
pass viewbag to partial view
pass viewdata to partial 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.

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.

Passing data from PartialView to its parent View, I want to pass values from a partialView (opened as a modal dialog) to the textbox in the parent view. The PartialView @model  Pass Data to Partial View using ViewBag/ViewData You can use ViewData / ViewBag to pass the information from the controller’s action method to the View. ViewData uses ViewDataDictionary and ViewBag is just a wrapper around ViewData using dynamic feature.

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.

Passing data from partial view to parent view, Passing value from partial view to parent view textbox. Pass data from the controller to the view using httppost. Passing variable from partial view model to parent view - ASP.NET. Asp.net-core pass data from view to controller and then another view. I am trying to create a partial view (re-used many times) that will prompt the user for data and return that value to the parent view. My best solution so far is to pass to the partial view the name of a hidden object (a div, for instance) in the parent view, that the partial view will then set teh value of when the user has selected or entered

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

MVC, My best solution so far is to pass to the partial view the name of a hidden object (a div, for instance) in the parent view, that the partial view will then set teh value of when the user has selected or entered a value. And then just pass the view model you want the partial view to render as a parameter using the overloads of the Partial method. @Html.Partial("RemoveContract", ViewBag.Query2) NOTE: Depending on your setup, you may need to use this.ViewBag.Query2 = query2.ToList() to materialize the query before passing it on to the view.

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 ;

Passing and data from partial view to Main View, could any one help me to get the data from partial view to main view in mvc 5. /​questions/35932728/passing-data-from-view-to-partial-view-in-mvc/35933874 /19766502/passing-data-from-partial-view-to-its-parent-view. Passing data to partial views By default, the view engine passes each partial view as a reference to the same view model object it received from the controller. It’s a simple matter of sharing the view context and including system-defined data dictionaries such as ViewData and ViewBag as well. Most of the time, this behavior is fine.

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

Pass Parameter Value from Parent Views to Controller for Populate , Suppose, I create a partial view for Year Control and on year type is different i.e. if year type is 1 then year range will be April to March, if year type  Access data from partial views. When a partial view is instantiated, it receives a copy of the parent's ViewData dictionary. Updates made to the data within the partial view aren't persisted to the parent view. ViewData changes in a partial view are lost when the partial view returns.

Passing Data To Partial Views That Submit In ASP.NET MVC 2 , Basically, I had a strongly-typed partial view that submitted data. the partial view also needed to pass some "loose" data from its parent view  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?

MVC C# .NET: Passing data from a child partial view to the parent , NET: Passing data from a child partial view to the parent page via ViewContext.​ViewBag. This example demonstrates how to set the page title  binding model data to partial view. Now, instead of creating a view for the student list, we can create a partial view for it. This can be useful in many other views, because it just displays students information. For example, we can use the same partial view, to list all the students of a particular standard or students of the same age, etc.

Tips and Tricks about Razor Partial Views, For Razor, a partial view is a .cshtml file located under the Views the data to pass to the partial view can be computed in the parent view or is  Passing variable from partial view model to parent view - ASP.NET Asp.net-core pass data from view to controller and then another view How can we Pass Data From Partial Views To Parent View Controller Action methods

Comments
  • What do you mean by "pass data from the Partial View to the parent" exactly?
  • Well in the example I gave, the idea is that the ID of the wrapping div is being set from the partial view.
  • I was thinking it more as meta-data being passed to the parent where the parent can make use of it - the PartialView wouldn't complain if that data isn't used so it still is generally independent and reusable - just with added sugar if the parent can make use of it. Clearly the data is being produced after the requirement for it has been called, but this is part of the problem!
  • See my updated answer. You could use HttpContext to share data. But once again, please don't do that, it's a terribly bad design. There are better ways to achieve your original goal.
  • Thanks for your update - it works a treat - though does feel a bit dirty! What better ways would you suggest?
  • I can't suggest a better way because I don't what you are trying to achieve/solve in the first place. In your question you only asked how to pass data from partial view to the parent view but you never explained why you need to do that in the first place.
  • @DarinDimitrov What would be the best away to use a reusable prompt that returns a value to the parent view? It seems like it would be bad design to copy and paste the same pop-up prompt code across many views.
  • Thanks, this is a good get-around. Ideally I'll be looking for a pure-asp.net answer but +1 all the same!