文件上传绑定到视图模型 [英] File upload bound to the Viewmodel

查看:136
本文介绍了文件上传绑定到视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在哪里,我上传多个文件的格式,并有几个文本框,并与每个文件被上传相关的一些复选框。
我已经看到示例上传多个文件,其中的ActionResult签名是这样的:

I have a form where I am uploading multiple files and there are a couple of textboxes and some checkboxes associated with each file being uploaded. I have seen examples for uploading multiple files where the actionresult signature is something like this:

[HttpPost]
public ActionResult Upload(IEnumerable<HttpPostedFileBase> fileUpload)

不过,我找不到,我可以有多个上传的文件在我的签名的ActionResult是这样的例子:

However, I cant find any example where I can have multiple files uploaded where my actionresult signature is something like this:

[HttpPost]
public ActionResult Upload(MyViewModel vm)

我想这个视图模型所发布的原因是因为我认为它更清洁比使用的FormCollection变量,因为我要被上载的每个文件及其相关的文本框一起添加的数据通过一种方式<$组合在一起C $ C>列表与LT; FileUploadPacket&GT; 这将视图模型的一部分​​

The reason I want this viewmodel being posted is because I think its cleaner than using the FormCollection variable and because I want to each file being uploaded and the data added along with its associated textboxes to be grouped together by way of a List<FileUploadPacket> which will part of the ViewModel

更新

我下面的视图模型:

public class EmployeeVM  
{

    public int EmployeeID {get ;set;}
    public string EmpName {get ;set;}
    //Other properties

    public List<FileUploadPacket> FileUploadPackets { get; set; }
}

这有一个类型的属性的FileUploadPacket类HttpPostedFileBase

The FileUploadPacket class which has the property of type HttpPostedFileBase

public class FileUploadPacket 
{

    public int FileID {get ;set;}
    public string UserEnteredDesc {get ;set;}
    //some more other properties

    public HttpPostedFileBase UpFile { get; set; }
}

$ C $我view.aspx章C片断如下

Code snippet of my view.aspx as below

<%: Html.TextBoxFor(model => model.EmpName, new { maxLength = 50 })%>

Upload your files here: 
<input type="file" id="UpFile" name="UpFile" value="ActionHandlerForForm"  />
<%: Html.TextBoxFor(model => model.FileUploadPackets[0].UserEnteredDesc )%>

<input type="file" id="UpFile" name="UpFile" value="ActionHandlerForForm"  />
<%: Html.TextBoxFor(model => model.FileUploadPackets[1].UserEnteredDesc )%>

正如你所看到的,我都具体到这一个文件的其他属性被上传保存在自己的类。所以,在我的形式,员工可以输入他的名字和他的上传文件,并提供了一​​些描述和其他细节为每个文件。如果我移动公共 HttpPostedFileBase UpFile {搞定;组; } 属性设置为 EmployeeVM 类,那么我将不得不单独收集所有的文件在一个数组和手动映射文件到其描述。有没有办法做到在 FileUploadPacket 类本身?

As you can see, I have all the other properties specific to this one file being uploaded kept in its own class. So that in my form an employee can enter his name and upload his files and provide some description and other details for each file. If I move the public HttpPostedFileBase UpFile { get; set; } property to the EmployeeVM class then I will have to collect all the files separately in an array and manually map a file to its description. Is there no way to do this keeping the UpFile property in the FileUploadPacket class itself?

我使用的ASPX视图引擎。

I am using the aspx view engine.

请帮忙。感谢您的时间...

Please help. Thanks for your time...

推荐答案

的修复这个正在改变你的名称和标识的上传控件。

The fix to this is changing the way you Name and ID the upload control.

<%: Html.TextBoxFor(model => model.EmpName, new { maxLength = 50 })%>

Upload your files here: 
<input type="file" id="FileUploadPackets[0].UpFile" name="FileUploadPackets[0].UpFile" value="ActionHandlerForForm"  />
<%: Html.TextBoxFor(model => model.FileUploadPackets[0].UserEnteredDesc )%>

<input type="file" id="FileUploadPackets[1].UpFile" name="FileUploadPackets[1].UpFile" value="ActionHandlerForForm"  />
<%: Html.TextBoxFor(model => model.FileUploadPackets[1].UserEnteredDesc )%>

这为我工作!希望它可以帮助其他人那里..

This worked for me!! Hope it helps anyone else out there..

这篇关于文件上传绑定到视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆