2

Please help : the validator does not validate a double value:

When I type a Double value with a dot or a vergule the validator displays me an error message that the value is not valid:

when I type a value with vergule he gives me this message: The LONGITUDE field must be a number.

while when I type a value with point he gives me this message: The value "230.5" is not valid for ATTITUDE.

See Exemple

this is the code of the view:

@model Miam.Models.diverse_info_commerce

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>diverse_info_commerce</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.ID_COMMERCE, "ID_COMMERCE", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("ID_COMMERCE", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.ID_COMMERCE, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.VILLE, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.VILLE, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.VILLE, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.RUE, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.RUE, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.RUE, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NUMERO, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.NUMERO, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NUMERO, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LONGITUDE, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LONGITUDE, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.LONGITUDE, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ATTITUDE, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ATTITUDE, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ATTITUDE, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.PAYS, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PAYS, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PAYS, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.HEUR_OUVERTURE, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.HEUR_OUVERTURE, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.HEUR_OUVERTURE, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.HEUR_FERMETURE, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.HEUR_FERMETURE, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.HEUR_FERMETURE, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group" hidden>
            @Html.LabelFor(model => model.CREATED_AT, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.CREATED_AT, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.CREATED_AT, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group" hidden>
            @Html.LabelFor(model => model.UPDATED_AT, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.UPDATED_AT, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.UPDATED_AT, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

The model:

namespace Miam.Models
{
    using System;
    using System.Collections.Generic;

    public partial class diverse_info_commerce
    {
        public int ID_POSITION { get; set; }
        public Nullable<int> ID_COMMERCE { get; set; }
        public string VILLE { get; set; }
        public string RUE { get; set; }
        public int NUMERO { get; set; }
        public double LONGITUDE { get; set; }
        public double ATTITUDE { get; set; }
        public string PAYS { get; set; }
        public System.DateTime HEUR_OUVERTURE { get; set; }
        public System.DateTime HEUR_FERMETURE { get; set; }
        public System.DateTime CREATED_AT { get; set; }
        public System.DateTime UPDATED_AT { get; set; }

        public virtual commerce commerce { get; set; }
    }
}

The controller:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "ID_POSITION,ID_COMMERCE,VILLE,RUE,NUMERO,LONGITUDE,ATTITUDE,PAYS,HEUR_OUVERTURE,HEUR_FERMETURE")] diverse_info_commerce diverse_info_commerce)
 {
       if (ModelState.IsValid)
       {
             diverse_info_commerce.UPDATED_AT = DateTime.Now;
             diverse_info_commerce.CREATED_AT= DateTime.Now;

             db.diverse_info_commerce.Add(diverse_info_commerce);
             await db.SaveChangesAsync();
             return RedirectToAction("Index");
        }

        ViewBag.ID_COMMERCE = new SelectList(db.commerces, "ID_COMMERCE", "TITRE_COMMERCE", diverse_info_commerce.ID_COMMERCE);
        return View(diverse_info_commerce);
 }
TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
  • This is may be due to the culture problem. `230,6` is actually a string that's why its showing the error message. Then `230.5` is a number and it should pass. Without accessing real code it will be tough to guess. – TanvirArjel Mar 04 '19 at 16:00
  • @Soukaina Benchekroun This link might help you https://stackoverflow.com/questions/7295385/asp-net-mvc3-double-validation-comma-point-null – Hashir Malik Mar 04 '19 at 18:26
  • @Soukaina How did you solve this? – MatterOfFact May 27 '19 at 10:01

0 Answers0