Render An aspx File (Menu Bar) in Razor View Layout Page - asp.net

I have been looking around looking for a solution for this issue but have yet to arrive to a solution that works. I was using this as a reference but couldn't get anything to work:What is the right way to '#include file' in MVC?
I have an MVC 4 project that uses a Razor for the views and I want to add an aspx page that contains a menu bar that I would to add to the _LayoutPage.cshtml. As of right now I have recreated the menu so it looks the same in my razor view but the issue is that when a change needs to be made to the menu.aspx page I have to make the same changes in my razor view.
So my goal is to just render this menu in my layout.cshtml page.
This is what i have right now but it does not work. Is there anyway to do this?
<div id="page-header">
#Html.RenderPartial("P:/menu/menu.aspx")
</div>

Place your .aspx file in Views foulder and then:
#Html.Partial("~/Views/WebForms/menu/menu.aspx")
Also, could be that it is not working becouse RenderPartial works other way, try:
#{ Html.RenderPartial("P:/menu/menu.aspx"); }

Related

can't create a razor page in asp.net

I'm trying to create new pages for my app in ASP.NET razor pages, but I can't see anything
trying to open it
I'm right clicking on the folder then clicking add,
then I'm clicking Razor Page and getting to this page
then I click add and get to this page
and here is where I'm getting stuck, because in that page there is no option to use Razor Pages.
is there any thing I'm doing wrong?
thank you.
When I choose Add=>Razor Page,I will get:
And when I choose Add=>New Item,I will get:
Maybe you can try to choose Add=>New Scaffolded Item,then select Razor Page in the menu:

What's the best way of structuring hiearchy of views in ASP.NET MVC?

I'm currently converting a ASP.NET webforms solution to MVC.
In webforms we besides the site.master we have master pages where we keep the common code and head imports for each section. So a normal page inheritance goes like so:
Site.master -> section.Master -> page.aspx
In MVC I'm unclear as to where to place the code common to a section. Is splitting the section content into partial views the only solution? So it becomes?
Layout.cshtml -> page.cshtml -> any-number-of-partial-views
To give an example, if I need a single CSS file to be shared across all 10 section pages, I can put it in a partial view and render it on each page but doesn't feel incredibly efficient. Or maybe I just need to get my head around to this new way of working.
You can use sections in MVC as well.
In your _Layout.cshtml you might have something like this:
#RenderSection("testSection",required:false)
And in your child view you can specify what HTML should be rendered in this section or you can leave it all together because required has been set to false:
#section testSection{
<h1>Test</h1>
}
Partial view is your big friend if you would like to reuse the code several places.
You can put partial view inside Views > ControllerNameAsFolderName > view pages (put here) to use only in same controller views or even inside Shared folder to use globally.
section is another option, this is like UserControl (in web forms). create section reference #RenderSection("sectionName", required:false) on Layout page and use it on view pages like
#section sectionName{
<div>content goes here</div>
}
Assume you want to put something (like meta infos) inside <head> from view pages, in this case you can create a section in _Layout.cshtml page and then use that section on view pages.
This way you can place your code at certain location in DOM structure from any page. section has a great feature that allow you to mark it as required:false/true.

In aspx with mvc 4, how to modify layout pages slightly on opening of a new page

When an htmlActionLink is clicked, a new page (say About) is opened with layout and the main body is rendered from the corresponding view. I want to change the style of the about link to show that this link is active. I tried a lot using jquery. It changes momentarily, but it comes back to its original style. I have not tried it through controller. I've used jquery in the layout.cshtml and about.cshtml page.
Look in to MvcSiteMap. You can use the MVC helpers to create a navigation menu that changes as you visit pages. Also, keeping it server side over using jQuery is going to be the best bet.

Hide content in master page from view page

I have a master page which is shared between about 20 views. In just one view, I need to make one small adjustment to the master page (I need to hide a textbox).
How can I include css or javascript in my view to acheive this?
Or is there some clever trick like including a conditional <% if (View.Name = "blah") { ... } %> that I can stick in my master page?
Thanks for any hints.
As you said you can write a JavaScript code to achieve your goal and include it only in one view. In one of my projects I do the same: I have jQuery AJAX request only at one view, so I just used script tags to include it

show an aspx page in content tag

I have a master page(MyMasterPage.master) with two content place holders. I wanna show another aspx page(MyHeader.aspx) in the 1st content place holder of a content page(MyContentPage.aspx) that uses my master page.
You should be using user controls for reusable components of a pages. Create a Header.ascx file instead of a .aspx. You can then drag that into your ContentPlaceHolder from the solution explorer when in design mode.
http://msdn.microsoft.com/en-us/library/y6wb1a0e.aspx
I would use UserControls. MyHeader.ascx.
I echo the prior commenter's suggestion to use a usercontrol if possible.
If you really want to keep your existing aspx page; you might want to take a look at using an IFrame to accomplish this.

Resources