Html.ActionLink("<span class=\"title\">Retry</span><span class=\"arrow\"></span>", "Login", "User")
Hi,
If I execute above code in ASP.Net MVC 2, I get the following output on my screen:
Error? http://img27.imageshack.us/img27/2069/screenerror.png
How do I disable the escaping of the code, so my span is within the ActionLink, and not displayed as output?
I know this is expected behavior, to keep it safe, but I want it to interpret the HTML code I pass as a parameter.
Thanks!
Yvan
The ActionLink helper method can only be used for plain-text links.
You should manually make an <a> tag, like this:
<a href="<%=Url.Action("Login", "User") %>">
<span class="title">Retry</span><span class="arrow"></span>
</a>
I think the following also works.
<span class="title"><%= html.actionlink("Retry","Login", "User") %></span><span
class="arrow"></span>
I mean, <.span>Retry<./span> is just <.span>plaintext<./span> which is the same as the actionlink text? ("." inserted for SO)
Related
I'm using the markup as follows.
#Html.ActionLink(#User.Identity.Name, "LogOut", "Account")
Now, I need to add a span inside the anchor because I want to use glyphs from Bootstrap. As far my googlearch went, there's no way to specify it using the helper above. So, I've redesigned it to explicit HTML as follows.
<a href="~/Account/LogOut">
<span class="glyphicon glyphicon-log-out"></span>
<span>#Global.LogOut #User.Identity.Name</span>
</a>
It works but the link isn't always targeting the same address as the first example does. It's because I've got en/ or se/ etc. for language first. When routing based on MVC, the language prefix stays in place but it gets lost when specifying the URL explicitly.
How can I specify a call to the specific action method?
Try:
<a href="#Url.Action("LogOut", "Account")">
<span class="glyphicon glyphicon-log-out"></span>
<span>#Global.LogOut #User.Identity.Name</span>
</a>
On view you can call MVC action with parameters like this:
Click Here;
On your controller side
public ActionResult FilterCustomerGrid(string filterName, string filterValue)
{
return View("ImportExcel");
}
I am displaying my 'news' page and I want the customer to be able to output some simple html.
My view looks like this:
#using SuburbanCustPortal.SuburbanService
<br />
#foreach (var item in (IEnumerable<TokenNews>) ViewBag.News)
{
<div class="fulldiv">
<fieldset>
<legend>#item.Title</legend>
<div>
#item.Body
</div>
</fieldset>
</div>
}
When I do it this away, the html isn't being rendered by the browser, it's just showing the html as text.
Any way that I can output the html where the browser will render it?
You didn't exactly specify what part is being shown as text, but if it's item.Body, do #Html.Raw(item.Body) instead. That turns a string into an IHtmlString, whose purpose is to tell Razor that this thing is guaranteed to be safe to output as-is, and will not contain nasties like XSS attacks (ensuring this when using Html.Raw is your job). Everything that is not an IHtmlString will be escaped automatically by Razor.
I haven't done MVC2 (or any MVC really) in over a year and while this works, I want to make sure it's the best way.
I have a "splash" page that allows the user to choose what they want to work on; Interfaces, Events, Parameters or Tasks. The HTML is:
<a href="/Interfaces/Index">
<div id="homeInterfaces" class="homeLinks">
<h1>Interfaces</h1>
<h2>Create, Edit or Delete Interfaces</h2>
</div>
</a>
<a href="/Tasks/Index">
<div id="homeTasks" class="homeLinks">
<h1>Tasks</h1>
<h2>Create, Edit or Delete Tasks</h2>
</div>
</a>
<a href="/Parameters/Index">
<div id="homeParameters" class="homeLinks">
<h1>Parameters</h1>
<h2>Create, Edit or Delete Parameters</h2>
</div>
</a>
<a href="/Events/Index">
<div id="homeEvents" class="homeLinks">
<h1>Events</h1>
<h2>Create, Edit or Delete Events</h2>
</div>
</a>
The links work and go to the appropriate pages, but for whatever reason, I'm not sure if this is the proper way to go about it. I have controllers for each, the proper folders with the Index views for each. I don't think I should be using Html.ActionLink, but maybe there's something else I should be using. Any thoughts? Thanks.
Yes, you should be using a helper for these. This will ensure that your generated links use the appropriate routes, which will make your life much easier in the future if you do anything fancy with your URLs or deploy to a non-root path (i.e. /)
In your case, the easiest solution is to replace the URL with #Url.Action. I'd use that over Html.ActionLink because Html.ActionLink doesn't let you easily linkify content more complicated than simple text (e.g. your html).
<a href="#Url.Action("Index", "Interfaces")">
<div id="homeInterfaces" class="homeLinks">
<h1>Interfaces</h1>
<h2>Create, Edit or Delete Interfaces</h2>
</div>
</a>
If you're not using Razor, go switch to that first. It's worth it. Or drop the # sign and add some <%= ... %>.
As #MichalFran notes, though, if it's working now, you can always just start following this advice in the future and revisit what you've already done when/if you need to down the road.
This answer is not standalone. I am posting it as a supplement to Michael Haren's answer, which I like.
The other thing I would like to add as a "thought" is T4MVC. I am a big fan of it, because it can help you get rid of more "magic strings" in both #Url.Action and #Html.Action. For example, take Michael's example code:
<a href="#Url.Action("Index", "Interfaces")">
...
Using T4MVC, you can get rid of the magic strings, and render the route URL like so:
<a href="#Url.Action(MVC.Interfaces.Index())">
Then, if you ever rename the InterfacesController or rename / change the signature of your Index() action method, the view will fail to compile instead of getting a 404 at runtime.
Another cool thing is that you can pass parameters to the routing engine as if you were invoking the action method:
public ActionResult ById(int entityId) { ... }
<a href="#Url.Action(MVC.Interfaces.Index(Model.EntityId))">
...
I don't know why more people don't post about this!
I have this tag in my menu
<li>Home</li>
and I was wanting to insert a class="selected" if the current controller was the HomeController. So I modified the tag to look like this.
<li><a href="/" #if (ViewContext.Controller.ToString().EndsWith("HomeController")) { Response.Write("class=\"selected\""); }>Home</a></li>
Now, I see the class="selected" appear at the top of the page and the rest of the markup is messed up. I just wanted to have the tag look like this
<li>Home</li>
If the current controller is the HomeController.
Any ideas what I did wrong?
Thanks!
Try
<a href="/" #if (ViewContext.Controller.ToString().EndsWith("HomeController")) { <text>class="selected"</text> }>Home</a>
Razor encodes everything by default, if you use the special tag <text> it renders the content as is.
Here is a quick reference guide on Razor syntax: http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx
I'm rendering some input controls dynamically. I wrote a method like this
public override MvcHtmlString GetActionControl(HtmlHelper<ItemDetailsViewModel> helper)
{
return
helper.EditorFor(A => A.Triage.ProcessNumber) +
helper.ValidationMessageFor(a => a.Triage.ProcessNumber, "*")
}
And I'm calling this from the main CSTHML file like this:
<!-- Render Action Controls for each possible action -->
#foreach (IActionControl iac in WorkflowActionControls.GetActionControls(Model.WorkflowItem.CurrentState))
{
<div id="#("div" + iac.ControlName)" class="ui-corner-all ui-widget ui-widget-content" style="margin-top: 24px; padding: 15px;">
#iac.GetActionControl(this.Html)
</div>
}
Notice that I'm passing the current HtmlHelper to the GetActionControl() method.
Problem is, the generated output is only:
<input class="text-box single-line" id="Triage_ProcessNumber" name="Triage.ProcessNumber" type="text" value="" />
Ie, no "extended" validation fields, and no validation . Aditionally I've discovered that the helper.ValidationMessageFor() method returns NULL.
However, If I put the editor and the validationMessage on the .cshtml directly everything works like a charm:
#Html.EditorFor(a => a.Triagem.ProcessNumber)
#Html.ValidationMessageFor(a => a.Triagem.ProcessNumber, "*")
What am I doing wrong here? Shouldn't the end result of calling HtmlHelper.EditorFor be the same? I'm sorry if this is a lame question but there isn't much information on generating HTML markup dynamically in regards to MVC 3.
I've checked the common pitfalls and everything is OK: validation enabled on web.config, the model class has the proper validation attributes, etc.
Any help would be greatly appreciated :)
PS: I've simplified the pasted code and ommited the form.
I don't know if this will help you, but validation is rendered when Html.ViewContext.FormContext is not null. When i had same problem, i just initialized form context with new FormContext(); and it worked