由于某种原因,当我在mvc3 razor中有隐藏字段时,我的表单帖子不工作。如果我删除隐藏字段,它可以正常工作,但我需要该字段。
下面是我的ProductsController post方法和razor view
@model CCSPurchasing_MVC.Models.AddNewProductModel
@using CCSPurchasing_MVC.Models
@using (Html.BeginForm("editImage", "Products", FormMethod.Post))
{
@Html.ValidationSummary(true)
@Html.HiddenFor(m => m.ImadeId)
<div class="editor-field">
<p style="margin-left: 300px; margin-right: 20px;">
@Html.LabelFor(m => m.ImageFile)
<input type="file" name="file" id="file" data-val="true" data-val-required="Product Image is required"/>
</p>
</div>
<input type="submit" value="Edit" />
}
[HttpPost]
public ActionResult editImage(AddNewProductModel newP, HttpPostedFileBase file)
{
db = new DBConnection();
if (file != null && file.ContentLength > 0)
{
newP.ImageName = Path.GetFileName(file.FileName);
newP.ImageType = file.ContentType;
newP.ImageSize = file.ContentLength;
newP.ImageFile = new byte[file.ContentLength];
file.InputStream.Read(newP.ImageFile, 0, file.ContentLength);
newP.ImageHeight = 200;
newP.ImageWidth = 200;
}
string result = db.editImage(newP);
return View("editImageUpdate");
}
就像这样让你的表单标签,我相信它也会为你工作,因为当我测试你的代码时它为我工作:
@using (Html.BeginForm("EditImage", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
}
你需要添加enctype = "multipart/form-data"也在你的代码中,如果你想提交文件与你的fileupload控件。