Reputation: 51114
I have the following markup:
@Html.TextBoxFor(m => m.Items[i].Rate, new { @class = "form-control text-center Rate", @readonly = Model.PreTenderLockedDown, title = "Rate" })
When the Rate
property is e.g. 12.45 in the db, the above TextBoxFor
renders the number with a comma separator and not the period I want. Can I do anything outside of setting the language settings on IIS?
Upvotes: -1
Views: 2125
Reputation: 12070
Try this:
web.config:
<system.web>
<globalization culture="en-US" uiCulture="en-US" />
</system.web>
You can also apply this for the view where you want to force this setting:
<%@ Page="" UICulture="en-US" Culture="en-US" %>
Alternatively, you can override default behavior of TextBoxFor by using a custom helper method. I used a similar one for DropDownListFor in some of my projects as shown below:
Helper Method:
public static class MyHelpers
{
//Custom HTML Helper method used for setting "class" and disabled attributes of MyDropdownlist
public class MySelectItem : SelectListItem
{
/* Since you are passing this data using ViewData, you don't have a limitation and
you can put anything there. I advise that you use ViewBag instead of ViewData. */
public string Class { get; set; }
public string Disabled { get; set; }
}
public static MvcHtmlString MyDropdownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<MySelectItem> list, string optionLabel, object htmlAttributes)
{
return MyDropdownList(htmlHelper, ExpressionHelper.GetExpressionText(expression), list, optionLabel, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString MyDropdownList(this HtmlHelper htmlHelper, string name, IEnumerable<MySelectItem> list, string optionLabel, IDictionary<string, object> htmlAttributes)
{
TagBuilder dropdown = new TagBuilder("select");
dropdown.Attributes.Add("name", name);
dropdown.Attributes.Add("id", name);
StringBuilder options = new StringBuilder();
// Make optionLabel the first item that gets rendered.
if (optionLabel != null)
options = options.Append("<option value='" + String.Empty + "'>" + optionLabel + "</option>");
foreach (var item in list)
{
if(item.Disabled == "disabled")
options = options.Append("<option value='" + item.Value + "' class='" + item.Class + "' disabled='" + item.Disabled + "'>" + item.Text + "</option>");
else
options = options.Append("<option value='" + item.Value + "' class='" + item.Class + "'>" + item.Text + "</option>");
}
dropdown.InnerHtml = options.ToString();
dropdown.MergeAttributes(new RouteValueDictionary(htmlAttributes));
return MvcHtmlString.Create(dropdown.ToString(TagRenderMode.Normal));
}
}
Upvotes: -1
Reputation: 164
You can use "Value" attribute of TextBoxFor markup.
@Html.TextBoxFor(m => m.Items[i].Rate, new { @class = "form-control text-center Rate", @readonly = Model.PreTenderLockedDown, title = "Rate", Value=String.Format("{0:0.###}", m.Items[i].Rate) })
Here "{0:0.###}" means that it will display 3 digits under decimal points.
Upvotes: 0
Reputation: 320
Using regex will be an option, if you are not interested in over engineering. Sample code like below would work. I haven't tested the code, its just for illustration.
@Html.TextBoxFor(m => Regex.Replace(m.Items[i].Rate.ToString(), @",(?<=\d,)(?=\d)", "."), new { @class = "form-control text-center Rate", @readonly = Model.PreTenderLockedDown, title = "Rate" })
https://stackoverflow.com/a/51221701/575924 use regex from here if possible.
Upvotes: 0