What's the syntax for displaying this? My program will always render « literally in the pagination helper I am creating, and since the switch to MVC3 there seems to be no way to even hack it with Eval( string.FromCharCode )
Just use the corresponding UTF-8 character and the HTML helper will take care of the encoding it:
#Html.RouteLink("«", new { action = "navigate", page = 1 })
#Html.RouteLink("»", new { action = "navigate", page = 3 })
As long as you are using UTF-8 as your character-set then showing the « directly (without encoding it) is perfectly valid XHTML.
Related
I am returning data from my DB with multiple phrases. One of them being the following text : Submitted an Idea
I want to make the "Idea" in any an all text a hyperlink, so I want to use a replace function in my razor view to replace the word "Idea" with my Html Helper:
#item.RewardType.Replace("Idea", #Html.ActionLink("Idea", "ChallengeIdea", "Ideas", new { id = item.fkiIdeaId }, null))
I've looked around a bit but can not really find anything. Someone suggested using #Url.Action - But the issue remains the same.
How do I do this ? Or is using an Html helper the wrong way of doing this ?
Thanks for any help.
You can try this:
#Html.Raw(item.RewardType.Replace("Idea", $"<a href='/ideas/challengeidea/{item.fkiIdeaId}'>Idea</a>"))
Or
#Html.Raw(item.RewardType.Replace("Idea", "Idea"))
Html helpers are there to help you in general situations. When they produce more complications than value, they have no use
<span>Submitted an Idea</span>
If you have RewardType in a resource and can not use plain html, you could set RewardType to "Submitted an Idea" And use string.format
I am trying to display content such as
<p>Text and link - #Html.ActionLink("Link", "Action")</p>
from a database, but if I use #Html.Raw then it doesn't render the link.
Is there anyway to do this?
You will need to use a Razor parser in order to achieve that. Checkout RazorEngine which could be used render the Razor markup to HTML.
print it on the backend using this:
string template = "Hello #Model.Name! Welcome to Razor!";
string result = Razor.Parse(template, new { Name = "World" });
pass the results to front end using a model which contains the result in a string property.
you will need to install the following:
Install-Package RazorEngine
I saw in a tutorial video that we should use Html.Encode in our views to prevent malicious injections. However, when the text that I'm encoding contains the ' character (for example Dog's) the output in the browser is Dog#39;s. I would have thought every potentially dangerous character would be remaped to some safe code that the browser would render correctly. Is this not the case? How can I get ' to show up in the browser but in an HTML safe way?
The # in Razor automatically encodes for you, meaning that you probably did a double encode.
Example:
#Html.Encode("This is \"safe\"")
is more or less the same as
#{Response.Write(Html.Encode(Html.Encode("This is \"safe\"")));}
Dunno if that last one works in Razor though.
If you are using ASP.NET MVC 2 <%: %> is already encoding the value for you
In Razor (MVC 3) # encodes the values for you so you do not need to wrap the output in Html.Encode
Make sure that you are not double encoding
I have actions that take string id parameters that are based on a username which can include characters that require encoding, for instance "user?1"
If I use ActionLink() to generate the links, passing the string without encoding, it generates a link like this: http:\\localhost\controller\action\user?1, and the action gets passed "user" as the id.
If I UrlEncode() the string before passing it to ActionLink, then the link generated is: http:\\localhost\controller\action\user%253f1 as ActionLink will then encode the '%' character for you. Besides this looking ugly, it then also generates a HTTP Error 400 - Bad Request when following the link which I've not yet tracked down the cause of.
Is there any way that I can generate the url like: http:\\localhost\controller\action\user%3f1?
How about removing the ? character or replacing it with something else like a dash (-) or underscore (_) ?
You should look in the Global.asax.cs file
add another route for your convenience, in this case, the ff. might work:
routes.MapRoute(
null,
"{controller}/{action}/user/{id}",
new { controller = "Home", action = "Index" }
);
I guess this is what you want, to separate action for each users, but i suggest you use cookie for this purpose.
PS: Remember to put that one on top of your default route since routing is trying to match from top to bottom.
you can create a jquery plugin that check all the links and replace the char that you need to replace with the new value.
and after apply this plugin to all the ActionLinks
I am noticing a weird issue when using ModelState.AddModelError to validate input on my forms. The output from Html.ValidationMessage is not the true HTML value but it's encoded value and so the CSS style is not applied to the error message.
Example:
private string errorMessage = "<span class=\"negative\">{0}</span><br class=\"hid\" />";
ModelState.AddModelError("title", String.Format(errorMessage, "Tab title is required"));
The output is shown as:
<span class="field-validation-error"><span class="negative">URL is Required</span><br class="hid" /></span>
This didn't use to be the case with their earlier beta's and I am not sure what approach to take here.
Thanks
Nick
There is another way to do it, too, without having to create your own extension.
Say for instance we have the following in one of our controllers:
ModelState.AddModelError("Name", "<b>Please Use a Valid Person Name</b>");
We can then do the following in our view:
#if(Html.ValidationMessageFor(x => x.Name) != null){
#Html.Raw(Html.ValidationMessageFor(x => x.Name).ToString())
}
The will prevent the error message of '<b>Please Use a Valid Person Name</b>' from being encoded.
Create your own extension method that mimics Html.VallidationMessage...?
I had to do something similar because the built in MVC validation stuff (ModelState, ValidationMessage etc etc) doesn't cater for pages that have more than one form on a page.