0

出于某种原因,当我在 mvc3 razor 中有隐藏字段时,我的表单帖子不起作用。如果我删除隐藏字段,它工作正常,但我需要那个字段。

下面是我的 ProductsController 发布方法和剃刀视图

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

1 回答 1

1

只需像这样制作您的表单标签,我相信它也会对您有用,因为它在我测试您的代码时对我有用:

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

如果要使用文件上传控件提交文件,还需要在代码中添加 enctype = "multipart/form-data"。

于 2013-05-01T06:58:15.607 回答