1

I have html form:

 @using (Html.BeginForm("ShowAddedProduct", "AddProductsDialog",FormMethod.Post,
        new {id="AddProdForm" }))
    {          
        <p>Price:</p>@Html.TextBoxFor(x => x.Price, new { type="number",Class = "EnterProductInfoField"});
        @Html.ValidationMessageFor(x => x.Price);
        <input id="submitValidation" type="submit" value="Add" />
    }

If i enter integer value in textbox like 700.It sends valid Price field for model to ShowAddedProduct Action method ,but when i enter decimal number like 422.65,it doesn't sends it and i get in action method Price=0.Type of Price is double

Here is ShowAddedProduct method code.

 [HttpPost]
        public ActionResult ShowAddedProduct(Product product, HttpPostedFileBase uploadedImage)
        {

            product.DateAdded = DateTime.Now;
            if (uploadedImage != null && uploadedImage.ContentLength > 0)
            {
                using (BinaryReader reader = new BinaryReader(uploadedImage.InputStream))
                {
                    product.Picture = reader.ReadBytes(uploadedImage.ContentLength);
                }
            }
            using (GoodsContainer1 container = new GoodsContainer1())
            {

                product.SubCategory = container.SubCategorySet.FirstOrDefault(x => x.Id == product.SubCategory_Id);
                if (product.Article != null
                    && product.Name != null
                    && product.Picture != null
                    && product.Price != 0)
                {
                    container.ProductSet.Add(product);
                    container.SaveChanges();                   
                    return PartialView("~/Views/AddProductsDialog/AddedProduct.cshtml",product);
                }
            }
            return RedirectToAction("AddProducts");
        }

Here is model code for html form.

  public partial class Product
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Product()
        {
            this.DescriptionParameters = new HashSet<DescriptionParameters>();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public string Article { get; set; }
        public double Price { get; set; }
        public byte[] Picture { get; set; }
        public System.DateTime DateAdded { get; set; }
        public int SubCategory_Id { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<DescriptionParameters> DescriptionParameters { get; set; }
        public virtual SubCategory SubCategory { get; set; }
    }
}
4
  • 1
    do you have an action with double input? share you controllers code for that action Commented Aug 10, 2016 at 21:17
  • Just added controller code and model code. Commented Aug 10, 2016 at 21:23
  • 1
    What is the culture on the server. Is it one that accepts a . (dot) as a decimal separator? Commented Aug 10, 2016 at 21:57
  • I changed Regional and Laguage culture settings,from , to . and it works.But i still don't understant,why it didn't work,even when i used ,. Commented Aug 11, 2016 at 5:31

2 Answers 2

1

It is because of mvc internal annotation. change

@Html.TextBoxFor(x => x.Price, new { type="number",Class = "EnterProductInfoField"})

to

@Html.EditorFor(x => x.Price, new { type="number",Class = "EnterProductInfoField"})
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for hint,finally it works validation,for "." and "," in number.But it didn't solve problem for me.
delete "type="number" .
1
@Html.TextBoxFor(x => x.Price, new { Class = "EnterProductInfoField"})

Delete type="number", because it sets your value as integer (not double)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.