ASP.NET MVC 3: Stack Overflow on a Html.Action? - asp.net-mvc-3

By definition I had to post this question. I received the error on this line using ASP.NET MVC 3 and ASPX form. Where's my typo?
<a href="<%= Html.Action("About", "Home") %>">
<img src="<%= Url.Content("~/Content/images/newfront_04.jpg") %>" /></a>
I created a blank project to compare:
Web Config is identical minus connection strings (including Views web.config)
Global.asax.cs including routes is identical minus namespace
Page directive is identical
Home Controller code is identical minus namespace
Taking this line out makes everything work
The entire page is html with the exception of the page directive and ContentPlaceHolders
This is the Site.Master file

Html.Action is actually rendering that action where you are putting that code, and it's causing reentrancy there. That is: it's calling the whole action and outputting the resulting view... not outputting the url.
What you probably wanted was Html.ActionLink (which renders the whole A tag for you), instead, or Url.Action, to just output the URL - rather than the action result again.

Try Url.Action instead of Html.Action
<a href="<%= Url.Action("About", "Home") %>">
<img src="<%= Url.Content("~/Content/images/newfront_04.jpg") %>" /></a>

Related

thymeleaf spring standard dialect: using template

Hello I have some problems with templating pages.
I am returning from controller a view called list:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorator="layout/template">
<div layout:fragment="pageContent">
<p>LIST</p>
</div>
</html>
And I would like to put this into template where I have a lot of html stuff and:
<div layout:fragment="pageContent">Demo static page content</div>
But I am getting in web browser only list view.
How to put one view returned from controller to template using SpringStandardDialect?
So if i understand correctly, you want to inject that fragment which is called pageContent into some other html page (lets call it main.html for sake of it.
First thing to do is change the div in list to the following:
<div th:fragment="pageContent">
<p>LIST</p>
</div>
then in your main.html you would call the fragment via:
<div th:include="list::pageContent"></div>
or
<div th:replace="list::pageContent"></div>
Btw "list::pageContent" indicates that its in base folder, if its located in folder called example then it would be "example/list:pageContent".
here is the great example on Thymeleaf website: http://www.thymeleaf.org/doc/usingthymeleaf.html#including-template-fragments
Hope this helps, if not let me know.

place span tag between anchor tag using Action Link Helper

I got website templates and using it for my website. I am new to asp.net mvc 3 and razor. It is very difficult to modify html tag using html helper. How can I place the span tag between anchor tag using ActionLink Helper. I have used razor and html helper for producing link.
I want to produce following tags:
<li><span class="glyph logout"></span> Logout</li>
I have try this
<li>#Html.ActionLink("<span class='glyph logout'></span> Log out", "LogOff", "Account") </li>
I am confuse how to do that. It is not the correct way that produces span tag as string. How can I produce correct tags.
What you want to use is #Url.Action to create the URL while having your custom HTML.
<li>
<a href="#Url.Action("LogOff","Account")" title="Logout">
<span class="glyph logout"></span> Logout
</a>
</li>
This way you have control over the URL and be able to add your own custom HTML. #Html.ActionLink doesn't allow you to add custom HTML inside the tags natively.
If you need to customize, what's inside your anchor tag, you should use the Html.Action method instead of the Html.ActionLink
<li>
<a href="#Html.Action("LogOff", "Account")" title="Logout">
<span class="glyph logout"></span> Log
</a>
</li>

ASP.NET MVC 3 - Placeholder

I'm working on an ASP.NET MVC 3 application. I primarily come from a ASP.NET WebForms background. I am working on an application with a complicated layout scheme. Because of this, i was hoping to have all of my layout code in, well, _Layout.cshtml. My challenge is, there is custom javascript logic associated with each page. I've found that if this JavaScript is included in the middle of my page, it doesn't work. So what I wanted to do was move it elsewhere. But in order to do this, I need something similar to the ASP.NET WebForms PlaceHolder control. Ideally, I would like to be able to do something like this:
<body>
<div id="myLayout" style="background-color:Gray; height:100%;">
<div id="myContent" style="background-color:Silver;">
#RenderBody()
</div>
<div id="myFooter" style="background-color:Silver;">
Footer
</div>
</div>
#RenderScript()
</body>
Is there a way for me to do this? Or am I going to have to write every page individually?
Thank you!
Here's what i do, in each of your views create a section like this, put any html you want in it
Any View:
#section Scripts
{
<script src="#Url.Content("~/Scripts/myscript.js")" type="text/javascript"></script>
<!-- Styles, more scripts, etc -->
}
Then back in your _Layout.cshtml you can render the section anywhere you want, the second parameter says if the page requires a Scripts section or not.
_Layout.cshtml: (anywhere you want)
<head>
#RenderSection("Scripts", false)
</head>

MVC wrap content with static content. UserControls?

I'm trying to wrap content with some static HTML in MVC and I'm not having any luck figuring this out. There must be an obvious solution that I'm not seeing, as this would seem like a desired feature.
In my View, I need to wrap HTML content with something:
<uc1:mc>
<p>Hello World!</p>
</uc1:mc>
That will render like:
<div class="ribbon">
<div class="left"></div>
<p>Hello World!</p>
<div class="right"></div>
</div>
With a Template like:
<div class="ribbon">
<div class="left"></div>
<%= IncomingMarkupGoesHere %>
<div class="right"></div>
</div>
The idea is to reuse html that wraps around other html.
I'm currently using two User Controls to achieve this, one with everything before and the other with everything after. Something like this:
<% Html.RenderPartial("RightRibbon_Start"); %>
Target Content...
<% Html.RenderPartial("RightRibbon_End"); %>
But this seems sloppy and isn't conducive to passing parameters that apply to HTML before and after the target content.
It's important that the content to be wrapped be allow to be written as multiple lined markup and not just a string, otherwise I would just pass the content as the Model.
What about creating helpers and passing the HTML in as an argument?
Passing html markup into an ASP.NET User Control

Image Pathing and Absolute Paths?

I'm developing a website in ASP .NET MVC 2 using C#.
I have a partial view, Header.ascx. In there I have an image for my website, MainImage.png.
When I use one of the primary Views I've created, the image shows up fine. For instance, I hit the Index ActionResult of my News Controller. (site.com/News)
However, when I dig deeper, I seem to lose my image, even though the Partial view is being displayed from the Master page. i.e., if I try going to site.com/News/Article/1
Are there any suggestions for keeping my image fully intact, such as a way to do absolute pathing?
The code I currently have in my partial view is here:
<div align="center"><img src="../Content/images/MainImage.png" style="border:none" /></div>
I've tried changing the src to ~/Content/images/MainImage.png but that breaks it all over the site.
Use the Url.Content helper method.
<img src="<%: Url.Content("~/content/images/imagename.png") %>" />
Same applies for when you want to include javascript files or css
<link rel="stylesheet" href="<%= Url.Content("~/content/site.css") %>" type="text/css" />
<script type="text/javascript" src="<%= Url.Content("~/content/scripts.js") %>"></script>
Whenever possible, make the path (href or src) to resource files, like images, CSS and JS relative to the web server root. That is, your URLs should begin with a slash:
<img src="/images/imagename.png" />
That format retains the current server address (which may be an IP address, an internal network address or any of a number of public web addresses), protocol and port, and doesn't depend on the apparent path of the page the user is looking at (which can change, depending on whether the user is accessing the page by its canonical location or by a URL rewrite).

Resources