English 中文(简体)
连接到查看模型的文件上传
原标题:File upload bound to the Viewmodel

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)

然而,我找不到任何例子, 我可以上传多个文件, 在我的动作中, 产生签字是这样的:

[HttpPost]
public ActionResult Upload(MyViewModel vm)

之所以我要张贴这个查看模型是因为我认为它比使用窗体集合变量更干净,还因为我想将每个文件上传,并添加数据及其相关文本框,以 List<FileUploadPacket> 组合起来,作为视图模式的一部分

<强> UPDATE

我的观点模式如下:

public class EmployeeVM  
{

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

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

文件 UploadPacket 类, 具有 Http PpostedFileBase 类型属性 。

public class FileUploadPacket 
{

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

    public HttpPostedFileBase UpFile { get; set; }
}

我视野的代码片片段。

<%: 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 )%>

您可以看到, 我拥有这个被上传到本类的文件的所有其它特性。 这样以我的形式, 员工可以输入他的姓名并上传他的文件, 并为每个文件提供一些描述和其他细节 。 如果我将公开的 < code> HtpPPPPedFileBase UpFileFile{ get; 设置; \\\ / code> 属性移到 < code> EmpleeeVM 类, 那么我就必须在阵列中单独收集所有的文件, 并手动绘制一个文件来描述它。 难道没有办法将 < code> UpFile 属性保留在 < code> FileUploadPacket 类本身吗?

我在用Aspx 查看引擎。

麻烦你帮忙,谢谢你抽空...

最佳回答

此选项的固定正在改变您的名称和标识上传控制的方式 。

<%: 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 )%>

这对我管用!

问题回答

GetHtml 助手不属于 mvc 框架的一部分, 您应该查找含有该助手的第三方库 。

上传属于 View 模式一部分的文件虽然简单, 但基本上是这样的

定义视图模型

public class MyViewModel 
{
     public HttpPostedFileBase MyFile { get; set; }
}

Inview/shared/EditorTemplates 内创建 MyViewModel.cshtml

<input type="file" id="MyFile" name="MyFile" />

与上传动作对应的视图

@model MyViewModel

@using(Html.BeginForm("Upload", "MyController", FormMethod.Post, new { enctype="multipart/form-data"})
{
     @Html.EditorForModel()
    <input type="submit" value="Upload" />
}

所需的属性对于上传文件很重要 。

因此,表格提交后,您应该看到 [HttpPost] action, vm.MyFile 内上传的文件。





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签