C# MVC Textbox MultiLanguage - asp.net-mvc

I have a view with a TextBox for input. However it seems like it doesn't support all Spanish characters. The upside down question mark doesn't seem to work. Is there a simple way to get around this?
<%= Html.TextBoxFor(m => m.Product.Name, new { style = "width:400px", maxlength = 150 })%>

Things to check:
Inside your web.config you are using utf-8:
<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>
Inside the <head> section of your site you have a <meta> tag:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Your view files are utf-8 encoded with BOM on the disk

Related

Pound symbol display

I have an mvc solution with a standard view using _Layout page for layout. Layout page has charset=utf-8 set in the header like so:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
For some reason, when I type "£" symbol in the view I get it displayed as "A£", with a dash above "A". At the same time when I put the same symbol in the _Layout page it's displaying fine. I got this resolved by using encoded value
£
but was wondering why does it happen?
It may have happened because of a mismatch between <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> and the encoding of your physical file.
E.g. Your file could be saved as ISO-8859-1.
I have read that note. I am using Adobe Dreamweaver for all my files and I think it is encoded in 'utf-8'.

Unable to decode a URL string encoded by MVC

I'm passing a string with a URL from the controller to the view (using a model).
While the string is in the controller, it is not encoded, but in the view the URL is encoded.
URL before encoding:
http://app.xpinator.com/FacebookPayments/FacebookDesktopAdData?paymentCode=s5usd920k&userLocale=en-us&ver=v3.0.1
URL after encoding:
http://localhost/FacebookPayments/FacebookPaymentItemData?paymentcode=v250usd45000k&userlocale=en-us&ver=v3.0.1
Relevant line in the view:
<meta property="og:url" content="#Model.URL" />
I want to display a decoded URL in the view. I tried using HttpUtility.HtmlDecode, HttpUtility.UrlDecode and Html.Raw - nothing worked.
Any ideas?
EDIT:
Thanks to Daniel's comment I realized that the encoding is happening only when the URL is in a meta property. when its a "displayable" HTML there is no encoding.
Anyway, still looking for a solution.
After realizing the problem occurs only with meta tags, I've found this thread:
Why is Html.Raw escaping ampersand in anchor tag in ASP.NET MVC 4?
I managed solve the problem with the suggested workaround to make the entire meta tag part of the raw value.
like this:
Controller:
model.URL = string.Format("{0}{1}{2}", "<meta property=\"og:url\" content=\"", paymentItemOG.URL, "\" />");
View:
<meta property="og:image" content="#Model.Image" />
#Html.Raw(Model.URL)
<meta property="og:description" content="#Model.Description" />
Page view-source:
<meta property="og:image" content="http://localhost/content/images/75x75_fullLogo.png" />
<meta property="og:url" content="http://localhost/FacebookPayments/FacebookPaymentItemData?paymentcode=v250usd45000k&userlocale=en-us&ver=v3.0.1" />
<meta property="og:description" content="texttexttext" />
Now it works great.

Encoding issue asp.net mvc 4

I´m trying to render a dynamic database title in asp.net mvc view. So I have something like this in my view.
#section meta {
<meta name="title" content="#Model.title" />
}
When model has special characters like Misión in spanish it shows in title something like
Misi&#243;n ... I´m using meta charset utf8 in my layout. Is there a special encoding I´m missing ?
How can I render Misión in title page ?
Using #someproperty will assume you're rendering out HTML and make sure it gets encoded to prevent things like cross site scripting. In this instance you want it to render the raw value, in which case you need to use Html.Raw(...) to render your content in it's raw form.
#section meta {
<meta name="title" content="#Html.Raw(Model.title)" />
}
However, just be aware that if the Model.title can come from user generated content (or some other untrusted source), you could be opening yourself up to security issues (for example if your Model.title's value was "test" /> <script ...etc...", a malicious user could use it to inject code into your pages.
Edit: Just including the content of my comment below for future googlers, since it appears that was the actual solution...
If you put the #Html.Raw(Model.title) directly in the page somewhere (i.e. not in the meta tag) and it works correctly there, you may be facing the same problem discussed here, in which case you could work around it by using the slightly uglier:
#section meta {
<meta name="title" #Html.Raw("content=\" + Model.title + "\"") />
}
Approach - 1
string value1 = "<html>"; // <html>
string value2 = HttpUtility.HtmlDecode(value1); // <html> //While getting
string value3 = HttpUtility.HtmlEncode(value2); // <html> //While saving
Approach - 2
Html.Raw("PKKG StackOverFlow"); // PKKG StackOverFlow

ASP.NET MVC 3 jquery : French accent characters are showing as #233 characters on screen

I have ASP.NET MVC 3 application having resource files in english and french.
A text 'Sélectionner la pharmacie' is stored in a french resource file.
When the value is read from resource files with razor syntax, it shows
'S#233;lectionner la pharmacie' instead of 'Sélectionner la pharmacie'.
e.g.
#MyResources.Strings_Resources.lbl_SelectPharmacy
Is there a way I can make it show the french accent characters ?
I suspect that your text is already encoded and razor is trying to encode it again (it encodes all outputs)
Try with
#Html.Raw(MyResources.Strings_Resources.lbl_SelectPharmacy)
First check your master page, you have set UTF-8
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<system.web>
<globalization enableclientbasedculture="true" uiculture="auto" culture="auto">
<!-- Use above or below <globalization> line, based on your site -->
<globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8"/>
</system.web>
If you have set this already then try below setup:-
<asp:Label Text="<%$ Resources:Strings, MyGlobalResource %>" runat="server" />
<asp:Label Text="<%$ Resources:MyLocalResource %>" runat="server" />
<%= HttpContext.Current.GetLocalResourceString("~/YOURASPXPAGE", "MyLocalResource", CultureInfo.CurrentUICulture) %>
Refer this URL for more info:-
ASP.NET MVC 3 Internationalization
ASP.NET MVC - Localization Helpers
I was having problem showing French characters in textarea through JQuery/Javascript (asp.net mvc).
myArry.push("\r\n" + "SÉRIE CERVICALE");
$('#myTextArea').val(myArry);
Result 🙁: SÉRIE CERVICALE
Adding this line in Web.config worked for me.
<system.web>
<globalization fileEncoding="utf-8" requestEncoding="utf-8"responseEncoding="utf-8"/>
</system.web>
Result 🙂: SÉRIE CERVICALE

Grails interprets and closes HTML meta tag

In my Grails GSP file I'm using the HTML meta tag:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
The problem is that Grails closes this tag and renders it as:
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
This fails W3C's HTML validation (since my doctype is HTML and not XHTML).
Is there a fix for this? How can I get Grails to not interpret the
meta tag?
I'm using grails-1.2-M4.
Follow up:
I create the Grails bug GRAILS-5696 for this issue.
Not sure that this is the most beautiful solution, but at least it will work for your case:
<%= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' %>
Well...this does not work since it is preprocessed by Grails before displayed as is.
So the only solution I see is to create a TagLib and output the content like this:
class MetaTagLib {
static namespace = 'my'
def meta = {
out << "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>"
}
}
and use it like:
<my:meta />
It works. Tested.
You could validate as HTML5 instead of HTML 4.01, by using <!DOCTYPE html> (that's it, really!). HTML5 allows trailing slashes even in the HTML syntax, in order to allow for systems like this that produce pseudo-XHTML.
Of course, HTML5 is not yet a finished standard; it may change. I think that this aspect of it is unlikely to be changed, but there is still some fairly contentious debate about a lot of the new HTML5 features, so keep in mind that it's not yet finalized.

Resources