Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC3 Modify culture for date and decimal through or not Visual Studio?

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!

like image 361
stuzzo Avatar asked Apr 21 '11 12:04

stuzzo


1 Answers

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

like image 56
Naraen Avatar answered Sep 30 '22 17:09

Naraen