出于某种原因,当我在mvc3 razor中有隐藏字段时,我的表单帖子不起作用。如果我删除隐藏的字段但我需要该字段,它工作正常。
以下是我的ProductsController post方法和razor视图
@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");
}
答案 0 :(得分:1)
只需制作这样的表单标签,我相信它也会对您有用,因为它在我测试您的代码时对我有用:
@using (Html.BeginForm("EditImage", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
}
如果要使用fileupload控件提交文件,还需要在代码中添加enctype =“multipart / form-data”。