我在我的数据库中上传图像文件时出错

时间:2018-03-29 11:18:59

标签: javascript jquery asp.net

我想在我的数据库中上传图片。当我在我的数据库中上传我的图像文件时,我收到错误

  

没有为此对象定义的参数较少的构造函数。

我用谷歌搜索了它,但我找不到任何适当的解决方案来解决这个错误。

这是我的观点;

@using (Html.BeginForm("AddProduct","Product",FormMethod.Post,new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()

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

                @Html.EditorFor(model => model.ProductName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ProductName, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">       
             @Html.LabelFor(mode=> Model.CategoryID, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.CategoryID, (SelectList)ViewBag.datafordropdown, new { @class = "form-control select2ddl" })

                @*@Html.EditorFor(model => model.CategoryID, new { htmlAttributes = new { @class = "form-control" } })*@
                @Html.ValidationMessageFor(model => model.CategoryID, "", new { @class = "text-danger" })
            </div>
        </div>

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

        <div class="form-group">

            @Html.LabelFor(model => model.Image, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Image, new { htmlAttributes = new { type = "file", @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Image, "", 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>
            @{ 
                if (ViewBag.message !=null)
                {
                    @ViewBag.message
                }
            }
        </div>
    </div>
}

这是我的控制器

[HttpPost]
public ActionResult AddProduct(ProductTable productTable, HttpPostedFile FileName) {
  var file = Request.Files[0];
  if (FileName != null) {
    if (FileName.FileName.EndsWith("jpg") || FileName.FileName.EndsWith("png") || FileName.FileName.EndsWith("bmp") || FileName.FileName.EndsWith("jpeg")) {
      try {
        string path = "~/Images/upload" + FileName.FileName;
        FileName.SaveAs(Server.MapPath(path));
        productTable.Image = path;
      } catch {
        productTable.Image = "~/Uploads/images/Students/default.png";
      }
    } else {
      productTable.Image = "~/Uploads/images/Students/default.png";
    }
  }
  try {
    db.ProductTables.Add(productTable);
    db.SaveChanges();
  } catch {
  }
  return View();
}

0 个答案:

没有答案