I've tried getting the following helper to work...but I'm scratching my head.
#helper SimpleHelper()
{
string message;
message="<b>Hello</b>";
#message
}
The text comes out improperly encoded. With the lt; and gt; instead of the html tags.
Neither WriteLiteral(message) or #:message work.
Use the Html.Raw() helper in Razor to display that as an un-encoded string.
Here's the Razor quick-reference that Phil Haack put together recently.
Related
The RenderAction is working just fine but as soon as I surround it with a if statement I get a compile error:
#if (#Model.IsConfigurationAllow)
{
#{ Html.RenderAction("Save"); } // CS1501: No overload for method 'Write' takes 0 arguments
}
More general question where can I found the grammar for the Razor view syntax?
Html.RenderAction renders the HTML directly into the response, so you cant call it in a code block.
The counterpart Html.Action returns a string with the results.
See http://haacked.com/archive/2009/11/17/aspnetmvc2-render-action.aspx
Did you try this?
#if (#Model.IsConfigurationAllow)
{
<text>#{ Html.RenderAction("Save"); }</text>
}
There are a few below (more can be found just by googling);
www.w3schools.com
A quick Reference
Introduction of Using Razor Syntax
I have the following doubt. I am considering the option to have html tags within my resx texts for localization.
When I put the resources directly I can resort to:
#Html.Raw(#Resources.ResourcesFM.Error_Email)
and it works as expected.
The problem is when the resource is being called by a validation message from an htmlhelper:
#Html.ValidationMessageFor(model => model.Email)
Got from an attibute:
[DataType(DataType.EmailAddress,
ErrorMessageResourceType = typeof(ResourcesFM),
ErrorMessageResourceName = "ErrorMailIncorr")]
What I am trying...
#Html.Raw(Html.ValidationMessageFor(model => model.Email))
I do not know how to get the same result as when using #html.Raw as the output from the helper is a MvcHtmlString...
Thanks
Try this:
View:
#Html.Raw(Server.HtmlDecode(#Html.ValidationMessageFor(m => m.UserName).ToString()))
Controller Action:
ModelState.AddModelError("UserName", "This is a link <a href='http://example.com'>Google Home</a>");
Html.ValidationMessageFor html-encodes the message. But you should be able to simply call HttpUtility.HtmlDecode() on the result. Even though the result contains html tags and whatnot, the decode will simply no-op on that part of the string.
So if `Html.ValidationMessageFor(...)' returns
<span><div>This is in a div</div<></span>
Then HttpUtility.HtmlDecode(Html.ValidationMessageFor(...).ToString()) will give you
<span><div>This is in a div</div></span>
It's not pretty, but it works. Your alternative is to recreate your own Validation helper version that never encodes the message.
For Localization, you may use String.Format and choose appropriate placeholder for the link
ModelState.AddModelError("UserName", String.Format("This is a link {0}", "<a href='http://example.com'>Appropriate String From a Resource</a>"));
This is probably a quick fix, but I am writing to a HtmlTextWriter in my HtmlHelper and no matter what I try it is encoding the HTML so the output shows the markup.
Helper:
protected override void WriteHtml(System.Web.UI.HtmlTextWriter writer)
{
writer.WriteFullBeginTag("div");
writer.WriteEndTag("div");
base.WriteHtml(writer);
}
and it will out put this to the page: <div></div>
EDIT
I am calling it in the view like this:
#Html.SAIF().Toolbar().Items(items => {
items.Add().Action("Action");
... ...
})
Edit #2
OK, let's see if I can explain this a little better.
If I use ViewContext.Writer to write my helper to the stream, when using a syntax like this:
#Html.MyHelper
It outputs encoded HTML (e.g.) <div>Test Div</div> = Not correct
If I use a syntax like this:
#{Html.MyHelper.Render();}
It renders correctly, meaning that it actually inserts the div into the DOM instead of displaying it as a string.
Make sense?
What is the best way to parse html tag like that:
Results
i just need to extract a href value.
I need to do this on controller.
I know i may use linq to xml for example or regex.
Any ideas what is the better way for that case?
May be there is any MVC helpers ready to go?
Basically what i need to do:
I have my extension method witch returning current url
public static string GetCurrentUrl(this ViewContext context)
{
string action = (string)context.Controller.ValueProvider.GetValue("action").RawValue;
string controller = (string)context.Controller.ValueProvider.GetValue("controller").RawValue;
if (action.ToLower() != "index")
return String.Format("/{0}/{1}", controller, action);
else if (action.ToLower() != "index" && controller.ToLower() != "home")
return String.Format("/{0}", controller);
else
return "/";
}
I need to compare this url with the value from a href like that Results
Use a XML parser and avoid regex. For this specific case XDocument seems easy enough:
var doc = XDocument.Parse("Results");
var href = doc.Element("a").Attribute("href").Value;
For more complex scenarios when HTML specific parsing is required and manipulating the DOM you could use HTML Agility Pack.
parsing html is notoriously difficult, there are so many hidden gotchas. The HTML Agility pack, which is a .NET HTML parsing library. Fizzler enables css selectors for html in c# and is built on top of the agility pack.
I've got the following custom html helper in asp.net mvc 3
public static string RegisterJS(this HtmlHelper helper, ScriptLibrary scriptLib)
{
return "<script type=\"text/javascript\"></script>\r\n";
}
The problem is that the result is getting html encoded like so (I had to add spaces to get so to show the result properly:
<script type="text/javascript"></script>
This obviously isn't much help to me.. Nothing I've read says anything about this.. any thoughts on how I can get my real result back?
You're calling the helper in a Razor # block or an ASPX <%: %> block.
These constructs automatically escape their output.
You need to change the helper to return an HtmlString, which will not be escaped:
return new HtmlString("<script ...");