ASP.Net MVC3通过Visual Studio修改日期和小数的文化?

时间:2011-04-21 12:58:17

标签: c# asp.net-mvc validation culture asp.net-mvc-3

我正在使用Razor开发MVC3应用程序。我在使用日期时遇到了麻烦,并在我的观点上加倍。我的Windows和Visual Studio都是英文版,但我想在“it-IT”中自定义项目的文化。 我发现了几篇文章如何管理不同的文化,通过JQuery Validation插件或直接在页面上设置文化,但我很困惑! 我想用于日期的格式是“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”是DB的双倍。注释[价格]由此帖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; }
    }

谢谢!

3 个答案:

答案 0 :(得分:5)

添加

<globalization culture="it-IT"/>

在system.web部分下的web.config中,做点什么?

编辑:

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

答案 1 :(得分:5)

你在他的博客上遇到了同样的问题吗?

http://haacked.com/archive/2011/03/19/fixing-binding-to-decimals.aspx

答案 2 :(得分:0)

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

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

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