Html表单不会向控制器发送双变量

时间:2016-08-10 21:16:23

标签: c# asp.net-mvc forms

我有html格式:

 @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" />
    }

如果我在700这样的文本框中输入整数值。它会将模型的有效Price字段发送到ShowAddedProduct操作方法,但是当我输入像422.65这样的十进制数字时,它不会#39 ; t发送它,我接受了行动方法Price = 0. Price的类型是double

这是ShowAddedProduct方法代码。

 [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");
        }

这是html表单的模型代码。

  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; }
    }
}

2 个答案:

答案 0 :(得分:1)

这是因为mvc内部注释。改变

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

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

答案 1 :(得分:1)

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

删除类型=&#34;数字&#34;,因为它将您的值设置为整数(不是双倍)