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; }
}
}
.(dot) as a decimal separator?,to.and it works.But i still don't understant,why it didn't work,even when i used,.