I have a formatted string variable to which passing to a ViewData Variable.
My Controller basically looks like this:
if(ModelState.IsValid)
{
string total = "$2.00";
ViewData["totalSales"] = total;
return View();
}
I have an html helper function in my view like this:
<%: Html.Label(ViewData["totalSales"].Tostring()) %>
however the asp.net mvc engine is generating this :
<label for="$2_00">00</label>
I just wanna pass the money value to the view and display it.
It appears that the decimal is breaking the value. However there has got to be a way around this.
Am I making a noob mistake here ?
Well I was able to bypass this by doing this:
<p><%: ViewData["totalSales"].ToString() %> </p>
But this does not explain why a decimal in a string is causing data loss.
Any ideas ?
Use String.Format() with currency
Your mistake is that the framework will automatically escape/transform special characters (such as the “.”) when they’re turned into HTML attributes. That’s by design.
Also, why are you passing a hard coded string via ViewData? Is that computed from your models? You may not be using Label correctly.