通过图像&从视图到控制器的数据

时间:2012-11-08 10:07:02

标签: asp.net-mvc-3 razor

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

我的观点

@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,所以数据不会从视图传递到控制器&amp;如果我删除它,则传递数据但不传递图像。

如何将两者一起发送?

2 个答案:

答案 0 :(得分:0)

试试这个

@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;
    }

答案 1 :(得分:0)

试试这个,   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; } }

相关问题