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"     }))
{
}

fileupload コントロールを使用してファイルを送信する場合は、コードに enctype = "multipart/form-data" も追加する必要があります。

于 2013-05-01T06:58:15.607 に答える