1

我想在提交按钮单击时将图像以及一些数据从视图传递到控制器。贝娄是我的代码

我的观点

@using (Html.BeginForm("AccountPhotoPost", "Post", FormMethod.Post, new {enctype = "multipart/form-data", accountId = Model.accountId }))
{
       <text>Post Photo : </text> <input type="file" name="file" id="file" />

       <input type="submit" value="Post Photo" id="saveButton"/>
}

我的控制器动作

 [HttpPost]
 public ActionResult AccountPhotoPost(HttpPostedFileBase file, long accountId)
    {

    }

这里的问题是,因为它是 FormMethod.Post ,所以数据不会从视图传递到控制器,如果我删除它,那么数据会传递但图像不会传递。

我怎样才能同时发送两个?

4

2 回答 2

1

试试这个

@model SomeModel
@using (Html.BeginForm("AccountPhotoPost", "Post", FormMethod.Post, new {enctype = "multipart/form-data"}))
{
       <text>Post Photo : </text> <input type="file" name="file" id="file" />

        @Html.HiddenFor(model => model.accountId )
       <input type="submit" value="Post Photo" id="saveButton"/>
}

在控制器中

[HttpPost]
 public ActionResult AccountPhotoPost(SomeModel model ,HttpPostedFileBase file)
    {
        var Id = model.accountId;
    }
于 2012-11-08T10:33:47.050 回答
1

试试这个

 HttpPostedFileBase hpf = Request.Files["file"] as HttpPostedFileBase;
                var httpPostedFileBase = Request.Files["file"];
                if (httpPostedFileBase != null && (hpf != null && httpPostedFileBase.ContentLength > 0))  
                {
                    var postedFileBase = Request.Files["file"];
                    if (postedFileBase != null)
                    {
                        fileName = postedFileBase.FileName;
                        BinaryReader reader = new BinaryReader(postedFileBase.InputStream);
                        byte[] attachmentBinary = reader.ReadBytes((int)postedFileBase.ContentLength);
                        hcUserReview.AttachmentByteValue = attachmentBinary;
                        hcUserReview.FileName = fileName;
                    }
                }
于 2015-08-18T07:30:58.743 回答