我该如何使某些特定的与InvariantCulture的模型领域,而其余的保持使用的用户文化? [英] How do I render only some specific model-fields with an InvariantCulture, while the rest keeps using the users culture?
问题描述
我有一个asp.net MVC视图几个隐藏的输入。它们的值包含类型双击
的对象。我希望他们能与 InvariantCulture的
被渲染,因为它们是用来被输送到一个API(谷歌地图)在客户端上。因为它是现在,他们得到渲染用逗号(,)作为小数点分隔符,而API期望一个点(。)作为小数点分隔符。
I have a couple of hidden inputs on an asp.net mvc view. Their values contain objects of type double
. I want them to be rendered with the InvariantCulture
as they are used to be fed to an api (google maps) on the client. As it is now, they get rendered with a comma (,) as decimal separator, while the api expects a point (.) as decimal separator.
的最好的解决办法是,如果我能在指定的模型上的属性 DisplayFormat
数据注解属性的文化,但我不认为这是可能的:
The nicest solution would be if I could specify a culture in the DisplayFormat
data annotation attribute on the property on the model, but I don't think that is possible:
public class Position
{
[DisplayFormat(DataFormatString="{0:G}, CultureInfo.InvariantCulture")]
public double Latitude;
...
}
我也不能只设置的CurrentCulture
到 InvariantCulture的
在我的的Application_Start
方法,因为在屏幕上它必须在正确的用户文化的其他值。
I can't also just set the CurrentCulture
to InvariantCulture
in my Application_Start
method, as there are other values on the screen which have to be in the proper user culture.
那么,有没有办法只是暂时改变目前的文化,我做了一个 Html.HiddenFor(型号=> Model.Latitude)权利之前
针对特定属性,然后然后再切?
So, is there a way to just temporarily change the current culture, right before I do an Html.HiddenFor(Model => Model.Latitude)
for that specific property, and then reset it afterwards?
还是有这样做的另一个更好的办法?什么是审议了有关这一最佳做法?
Or is there another better way of doing this? What is considered best practice concerning this?
推荐答案
我结束了一个特定的子对象创建一个模板(Position.chtml)位置
在我模式是这样的:
I ended up creating a template (Position.chtml) for a specific sub-object Position
in my model like this:
@model Models.Position
@{
var latitude = string.Format(System.Globalization.CultureInfo.InvariantCulture,
"{0:G}", Model.Latitude);
var longitude = string.Format(System.Globalization.CultureInfo.InvariantCulture,
"{0:G}", Model.Longitude);
}
@Html.Hidden("Latitude", latitude)
@Html.Hidden("Longitude", longitude)
<p />
<div id="mapCanvas"></div>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/maps.js")"></script>
<script type="text/javascript">
$(function () {
attachMap($('#mapCanvas'), $('#Position_Latitude'), $('#Position_Longitude'), true);
})
</script>
这种方式,我不需要任何属性添加到模型类。
This way, I don't need to add any attributes to the model class.
我不知道,但...这是很好的做法,包括像这个JavaScript?我不知道任何其他方式做到这一点(除非它包括在母版页,这是我不想做的,因为我并不需要它的所有网页上),而无需重复自己。我想保持这一干...
I'm wondering though... is it good practice including javascript like this? I don't know of any other way to do it (except including it in the master page, which I don't want to do as I don't need it on all pages) without having to repeat myself. I'd like to keep this DRY...
同时,(我可以更好地问这一个新的问题):因为它是现在,我有这个模板2次,一次是作为一个编辑模板,一旦作为显示模板。唯一的区别是,最后一个参数 attachMap
已经为真正
为编辑器模板和假
的显示模板。有一个简单的方法,使这种干燥的?
Also (I might better ask a new question for this): As it is now, I have this template 2 times, once as an editor template, once as a display template. The only difference is that the last parameter to attachMap
has to be true
for the editor template and false
for the display template. Is there an easy way to make this DRY?
这篇关于我该如何使某些特定的与InvariantCulture的模型领域,而其余的保持使用的用户文化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!