ASP.Net MVC3 通过或不通过 Visual Studio 修改日期和小数点的区域性?

发布于 2024-11-02 20:21:50 字数 7945 浏览 0 评论 0原文

我正在使用 Razor 开发 MVC3 应用程序。我在使用日期和双打我的观点时遇到麻烦。我的 Windows 和 Visual Studio 都是英文的,但我想在“it-IT”中自定义我的项目的文化。 我找到了几篇如何管理不同文化的文章,通过 JQuery 验证插件或直接在页面上设置文化,但我很困惑! 我想要使​​用的日期格式是“DD/MM/YYYY”,而双精度格式是 1.234,32。那么,

  • 我如何告诉 Visual Studio 如何使用这些格式(我检查了 Visual Studio 的设置,只有英语)?
  • MVC 注入代码来验证我的字段,如何更改它?
  • 最好的方法是什么?

显然任何建议将不胜感激。 我发布了我的项目的代码:

Offerta.cs

[MetadataType(typeof(Offerta_Validation))]
public partial class Offerta
{
}

public class Offerta_Validation
{
    [HiddenInput(DisplayValue = false)]
    public int IDOfferta { get; set; }

    [StringLength(300, ErrorMessage = "Campo troppo lungo")]
    [Required(ErrorMessage = "Campo obbligatorio")]
    public string Titolo { get; set; }

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:N2}")]
    [Required(ErrorMessage = "Campo obbligatorio")]
    [Price(MinPrice=0.01)]
    public decimal PrezzoIniziale { get; set; }

    [Required(ErrorMessage = "Campo obbligatorio")]
    [Integer]
    public int BuoniScontiMinimo { get; set; }


    public string Sconto { get; set; }

    [Date]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
    public System.DateTime DataAttivazione { get; set; }

    [DataType(DataType.MultilineText)]
    [AllowHtml]
    public string Sintesi { get; set; }

    [DataType(DataType.MultilineText)]
    [Required(ErrorMessage = "Inserire le condizioni")]
    [AllowHtml]
    public string Condizioni { get; set; }

    [DataType(DataType.MultilineText)]
    [Required(ErrorMessage = "Inserire la descrizione")]
    [AllowHtml]
    public string Descrizione { get; set; }
}

字段“PrezzoIniziale”是数据库上的双精度。注释 [Price] 由这篇文章 http://haacked.com/archive/2009 得出/11/19/aspnetmvc2-custom-validation.aspx。这是我的观点:

@model ManagerEmail.Models.OffertaFormViewModel

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.global.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.glob.it-IT.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/tiny_mce/tiny_mce.js")"></script>
<script type="text/javascript">
    tinyMCE.init({
        // General options
        mode: "textareas",
        theme: "advanced",
        plugins: "style,searchreplace,paste",

        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
        theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,forecolor,backcolor,|,styleprops",
        theme_advanced_buttons3 : "hr,removeformat,visualaid,|,sub,sup,|,charmap,|,link,unlink,anchor,image,cleanup,help,code",
        theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left",
        theme_advanced_statusbar_location : "bottom",

        // Skin options
        skin : "o2k7",
        skin_variant : "silver",

        width: "510",
        height: "300",
        object_resizing: false,

    });
</script>

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "editForm" })){
    @Html.ValidationSummary(true)
    <fieldset>
        <legend></legend>

        <ul>
            <li>
                @Html.LabelFor(model => model.Offerta.Titolo)
                @Html.EditorFor(model => model.Offerta.Titolo) <br />
                @Html.ValidationMessageFor(model => model.Offerta.Titolo)
            </li>
            <li>
                @Html.LabelFor(model => model.Offerta.PrezzoIniziale, "Prezzo iniziale")        
                @Html.EditorFor(model => model.Offerta.PrezzoIniziale) <br />
                @Html.ValidationMessageFor(model => model.Offerta.PrezzoIniziale)
            </li>
            <li>
                @Html.LabelFor(model => model.Offerta.BuoniScontiMinimo, "Minimo Buoni")
                @Html.EditorFor(model => model.Offerta.BuoniScontiMinimo) <br />
                @Html.ValidationMessageFor(model => model.Offerta.BuoniScontiMinimo)
            </li>
            <li>
                @Html.LabelFor(model => model.Offerta.Sconto)
                @Html.EditorFor(model => model.Offerta.Sconto) <br />
                @Html.ValidationMessageFor(model => model.Offerta.Sconto)
            </li>
            <li>
                @Html.LabelFor(model => model.Offerta.DataAttivazione, "Data Attivazione")
                @Html.EditorFor(model => model.Offerta.DataAttivazione) <br />
                @Html.ValidationMessageFor(model => model.Offerta.DataAttivazione)
            </li>
            <li>
                <label for="ddlAffiliato">Affiliato</label>
                @Html.DropDownListFor(model => model.Offerta.Affiliato.IDAffiliato, new SelectList(Model.Affiliati, "IDAffiliato", "RagioneSociale"), new { @id = "ddlAffiliato"} )
            </li>
            <li>
                @Html.LabelFor(model => model.Offerta.Condizioni)  <br />           
                @Html.TextAreaFor(model => model.Offerta.Condizioni) <br />
                @Html.ValidationMessageFor(model => model.Offerta.Condizioni, "", new { style = "top: -30px; left: 210px" })
            </li>
            <li>
                @Html.LabelFor(model => model.Offerta.Sintesi)  <br />           
                @Html.TextAreaFor(model => model.Offerta.Sintesi) <br />
                @Html.ValidationMessageFor(model => model.Offerta.Sintesi)
            </li>
            <li>
                @Html.LabelFor(model => model.Offerta.Descrizione)  <br />           
                @Html.TextAreaFor(model => model.Offerta.Descrizione) <br />
                @Html.ValidationMessageFor(model => model.Offerta.Descrizione, "", new { style = "top: -30px; left: 210px" })
            </li>
        </ul>
        @Html.HiddenFor(model => model.Provincia.IDProvincia)                 
    </fieldset>
    <p>  
        @Html.AntiForgeryToken()
        <input type="submit" value="Salva" id="submitButton"/>
    </p>
}

<script type="text/javascript">

    $("#Offerta_PrezzoIniziale").removeAttr("data-val-number"); //I forced this because MVC adds the data-val-number, so it doesn't accept double values :(

    $(function () {
        jQuery.global.preferCulture("it-IT");        
    });

    $("#submitButton").click(function () {
        tinyMCE.triggerSave();
    });


</script>
<script type="text/javascript">
    $(function () {
        jQuery.validator.addMethod("price", function (value, element, params) {
            if (this.optional(element)) {
                return true;
            }

            if (value > params.min) {
                var cents = value - Math.floor(value);
                if (cents >= 0.99 && cents < 0.995) {
                    return true;
                }
            }

            return false;
        });
    });
</script>

这是我的 FormViewModel

public class OffertaFormViewModel
    {
        public Provincia Provincia { get; set; }
        public Offerta Offerta { get; set; }
        public IEnumerable<Affiliato> Affiliati { get; set; }
    }

谢谢!

I'm working on an MVC3 application with Razor. I'm having troubles using Dates and doubles on my views. Both my Windows and Visual Studio are English, but I want to customize the culture of my project in "it-IT".
I found several articles how to manage different cultures, through the JQuery Validation Plugin or directly setting the culture on the page, but I'm a lot confused!!!
The format I want use for dates is "DD/MM/YYYY", while for double is 1.234,32. So,

  • How can I tell to Visual Studio how to use these formats (I checked on the Visual Studio's settings and there is only English)?
  • MVC injects code to validate my fields, how to change it?
  • What is the best approach?

Obviously any suggest would be appreciated.
I post the code of my project:

Offerta.cs

[MetadataType(typeof(Offerta_Validation))]
public partial class Offerta
{
}

public class Offerta_Validation
{
    [HiddenInput(DisplayValue = false)]
    public int IDOfferta { get; set; }

    [StringLength(300, ErrorMessage = "Campo troppo lungo")]
    [Required(ErrorMessage = "Campo obbligatorio")]
    public string Titolo { get; set; }

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:N2}")]
    [Required(ErrorMessage = "Campo obbligatorio")]
    [Price(MinPrice=0.01)]
    public decimal PrezzoIniziale { get; set; }

    [Required(ErrorMessage = "Campo obbligatorio")]
    [Integer]
    public int BuoniScontiMinimo { get; set; }


    public string Sconto { get; set; }

    [Date]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
    public System.DateTime DataAttivazione { get; set; }

    [DataType(DataType.MultilineText)]
    [AllowHtml]
    public string Sintesi { get; set; }

    [DataType(DataType.MultilineText)]
    [Required(ErrorMessage = "Inserire le condizioni")]
    [AllowHtml]
    public string Condizioni { get; set; }

    [DataType(DataType.MultilineText)]
    [Required(ErrorMessage = "Inserire la descrizione")]
    [AllowHtml]
    public string Descrizione { get; set; }
}

The field "PrezzoIniziale" is a double on DB. The annotation [Price] derive by this post http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx. This is my view:

@model ManagerEmail.Models.OffertaFormViewModel

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.global.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.glob.it-IT.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/tiny_mce/tiny_mce.js")"></script>
<script type="text/javascript">
    tinyMCE.init({
        // General options
        mode: "textareas",
        theme: "advanced",
        plugins: "style,searchreplace,paste",

        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
        theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,forecolor,backcolor,|,styleprops",
        theme_advanced_buttons3 : "hr,removeformat,visualaid,|,sub,sup,|,charmap,|,link,unlink,anchor,image,cleanup,help,code",
        theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left",
        theme_advanced_statusbar_location : "bottom",

        // Skin options
        skin : "o2k7",
        skin_variant : "silver",

        width: "510",
        height: "300",
        object_resizing: false,

    });
</script>

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "editForm" })){
    @Html.ValidationSummary(true)
    <fieldset>
        <legend></legend>

        <ul>
            <li>
                @Html.LabelFor(model => model.Offerta.Titolo)
                @Html.EditorFor(model => model.Offerta.Titolo) <br />
                @Html.ValidationMessageFor(model => model.Offerta.Titolo)
            </li>
            <li>
                @Html.LabelFor(model => model.Offerta.PrezzoIniziale, "Prezzo iniziale")        
                @Html.EditorFor(model => model.Offerta.PrezzoIniziale) <br />
                @Html.ValidationMessageFor(model => model.Offerta.PrezzoIniziale)
            </li>
            <li>
                @Html.LabelFor(model => model.Offerta.BuoniScontiMinimo, "Minimo Buoni")
                @Html.EditorFor(model => model.Offerta.BuoniScontiMinimo) <br />
                @Html.ValidationMessageFor(model => model.Offerta.BuoniScontiMinimo)
            </li>
            <li>
                @Html.LabelFor(model => model.Offerta.Sconto)
                @Html.EditorFor(model => model.Offerta.Sconto) <br />
                @Html.ValidationMessageFor(model => model.Offerta.Sconto)
            </li>
            <li>
                @Html.LabelFor(model => model.Offerta.DataAttivazione, "Data Attivazione")
                @Html.EditorFor(model => model.Offerta.DataAttivazione) <br />
                @Html.ValidationMessageFor(model => model.Offerta.DataAttivazione)
            </li>
            <li>
                <label for="ddlAffiliato">Affiliato</label>
                @Html.DropDownListFor(model => model.Offerta.Affiliato.IDAffiliato, new SelectList(Model.Affiliati, "IDAffiliato", "RagioneSociale"), new { @id = "ddlAffiliato"} )
            </li>
            <li>
                @Html.LabelFor(model => model.Offerta.Condizioni)  <br />           
                @Html.TextAreaFor(model => model.Offerta.Condizioni) <br />
                @Html.ValidationMessageFor(model => model.Offerta.Condizioni, "", new { style = "top: -30px; left: 210px" })
            </li>
            <li>
                @Html.LabelFor(model => model.Offerta.Sintesi)  <br />           
                @Html.TextAreaFor(model => model.Offerta.Sintesi) <br />
                @Html.ValidationMessageFor(model => model.Offerta.Sintesi)
            </li>
            <li>
                @Html.LabelFor(model => model.Offerta.Descrizione)  <br />           
                @Html.TextAreaFor(model => model.Offerta.Descrizione) <br />
                @Html.ValidationMessageFor(model => model.Offerta.Descrizione, "", new { style = "top: -30px; left: 210px" })
            </li>
        </ul>
        @Html.HiddenFor(model => model.Provincia.IDProvincia)                 
    </fieldset>
    <p>  
        @Html.AntiForgeryToken()
        <input type="submit" value="Salva" id="submitButton"/>
    </p>
}

<script type="text/javascript">

    $("#Offerta_PrezzoIniziale").removeAttr("data-val-number"); //I forced this because MVC adds the data-val-number, so it doesn't accept double values :(

    $(function () {
        jQuery.global.preferCulture("it-IT");        
    });

    $("#submitButton").click(function () {
        tinyMCE.triggerSave();
    });


</script>
<script type="text/javascript">
    $(function () {
        jQuery.validator.addMethod("price", function (value, element, params) {
            if (this.optional(element)) {
                return true;
            }

            if (value > params.min) {
                var cents = value - Math.floor(value);
                if (cents >= 0.99 && cents < 0.995) {
                    return true;
                }
            }

            return false;
        });
    });
</script>

And this is my FormViewModel

public class OffertaFormViewModel
    {
        public Provincia Provincia { get; set; }
        public Offerta Offerta { get; set; }
        public IEnumerable<Affiliato> Affiliati { get; set; }
    }

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

椵侞 2024-11-09 20:21:50

在 system.web 部分下添加

<globalization culture="it-IT"/>

web.config 是否可以解决问题?

编辑:

添加相关的 MSDN 链接
http://msdn.microsoft.com/en-us/library/bz9tc508.aspx

Does adding

<globalization culture="it-IT"/>

in your web.config under the system.web section, do the trick?

Edit:

Adding relevant MSDN link
http://msdn.microsoft.com/en-us/library/bz9tc508.aspx

分开我的手 2024-11-09 20:21:50

你的问题有点不清楚。据我了解,您希望应用程序以正确的文化显示,而不是 Visual Studio。

要设置日期和双精度格式(即文化),您可以在应用程序 Global.asax Application_Start() 事件中执行类似的操作:

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("it-IT");

Your question is a bit unclear. From what I can understand you want your application to display in correct culture, not your Visual Studio.

To set date and double format (ie. Culture) you would do something like this in your applications Global.asax Application_Start() event:

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("it-IT");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文