How to pass data from partial view to layout page - asp.net-mvc

I have a table of menu and use partial view to show it but my problem is that how to show this menu in all pages of website or how to show the partial view content in layout page.thnx

In your _PublicLayout.cshtml file, (this is the file every other page uses in your project. The file has a #RenderBody() section, so every view you make, will appear there.
But if you want to make a partial view and it needs to be displayed on all pages of your project, you can place that in the _PublicLayout file.
So for your navigation, make the partial view, and add it like this in your _layout.cshtml file:
<div id="nav">#Html.Partial("_NameOfTheFile")</div>
<div id="body"></div>
<div id="footer"></div>

Related

MVC Areas Overlayed Shared Layouts

I'm using Visual Studio to make an MVC App with sub-apps in Areas. In the root _Layout.cshtml I have a navbar-fixed-top that will have navigation to the different areas. I want this perpetuated on every single view. Then I modified the css to create a navbar-fixed-left where I will have navigation within just the particular area and that needs to be unique per area, but used on all views within that area.
So my question is, is there a way to use a shared layout within a shared layout so I don't redundantly copy and eventually screw up the top nav?
You can use the RenderSection('leftNavigation', false) in the html of your _layout page.
from the view that has left navigation you can fill the section
#section leftNavigation
{
<ul>
Some line
</ul>
}
you can use the #section in the _viewStart.cshtml of the area is you have navigation that is valid for the complete area.

Scripts not rendering in partial view

I have a view containing two partial views in it.
#{
ViewBag.Title = "Expenses";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div id="expensesreportchart">
#Html.Action("_SalesAndExpenses", "Reports")
</div>
<div id="expensesreports">
#Html.Action("_Expenses", "Reports")
</div>
Note-
Both partial views are created without using layout. As far as i know that when we use partial views in a view (using layout). Then scripts are obtained in partial views using parent view (the one created with using layout).
So My flow of using partial views in a view is like now-
View(using layout)--> partial view(without layout) + partial view(without layout)
Problem-
I don't get any problem in rendering page. I mean It renders partial view's layout using CSS, But doesn't call scripts from parent view.
I tried-
Because Scripts were not working so I had to use scripts again in partial view whether those scripts are used in parent view(the one with layout)
How do i use a partial view so that scripts and everything that i use in parent view should also remain in partial views.
since a partial view isn't loaded when the page loads the scripts on the main page won't trigger for the partial unless you put the functions in a main function that is called after the partial load or you tie the click events to the document
$(document).on('click', '.btnSubmit', function(){
//your code here
});
if you have a button on your partial with class="btnSubmit" this code will trigger on the main page when it is clicked

Rendering two views at the same time, or a partial and a view

I'm working on a site right now where I have a standard layout, with a view/template. I've spent the past 2 weeks trying to get ajax functionality working, but it's not working and I need an alternative. Is it possible to render a partial WITHIN a view?
As in, layout (view (partial)) sort of thing?
Right now I just have a standard layout and view with a yeild in the layout. There is an empty div in the view where the ajax HTML was supposed to go, I want to fill it with a partial for now
Yes surely, you can call a partial inside any view file. For example, you have a view file 'index.html.erb' and you want to call a partial '_new_partial.html.erb'
In your view file.
<div>
<%= render :partial => 'new_partial' %> #provide complete path if partial is not in the same directory.
</div>

Can I conditionally render partial views in _Layout.cshtml?

Suppose I have a _Layout.cshtml where I render a left sidebar, which is common to every page of my website.
Something along these lines - a menu, for example
<div id="left-sidebar">
#Html.Action("_MenuView", "LeftSideMenu")
</div>
A feature I would like to have would be to add another partial view, but only display it in certain sections of the website.
For example, in the blog section I may want to display a list of post categories or a treeview of the posts.
<div id="left-sidebar">
#Html.Action("_MenuView", "LeftSideMenu")
#if ("???")
{
#Html.Action("_BlogTreeView", "BlogEntries")
}
</div>
How could I do that? I know that I want to display "_BlogTreeView" if the view I'm rendering is returned by BlogController ... where do I go from there?
In your layout, add this section
#RenderSection("blogEntries", false)
Then in every view where you want to show the partial view add this:
#section blogEntries {
#Html.Action("_BlogTreeView", "BlogEntries")
}

mvc3 httpResponse.write

I have some partial views that are rendered sequentially. I would like to create a log of what has rendered successfully on the top of the page, when the last partial view has completed. The only way I can think of, is to keep an array of log details and then display the log detail in a partial view on the top of the page, using a rendersection.
This to me seems cumbersome, I would prefer to be able to use HttpResponse.Write to display it to the page. However this way I don't seem to be able to control the ordering of where my log will appear on the screen. I would like them to be displayed in the same location on the page. Is there a more elegant solution?
Why not put div in the top of the page and every Partial View write to that div?
<div id=log > <label id="logs"> The Log: </label> </div>
$('#logs').after('what you want1'); // from partial 1
$('#logs').after('what you want2'); // from partial 2
$('#logs').after('what you want3'); // from partial 3
$('#logs').after('what you want4'); // from partial 4
Pass the partial view the data you want to "log" in the Model\ ViewBag and start logging!

Resources