2012-11-08 183 views
0

我希望将图像以及视图中的一些数据传递给控制器​​。 贝娄是我的代码将图像和数据从视图传递到控制器

我查看

@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,数据不会从视图中传递,如果到控制器&我删除它然后数据通过但图像不通过。

如何将两者一起发送?

回答

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

相关问题