Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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)

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)

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

UPDATE

My View model below:

public class EmployeeVM  
{

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

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

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

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

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?

I am using the aspx view engine.

Please help. Thanks for your time...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
622 views
Welcome To Ask or Share your Answers For Others

1 Answer

GetHtml helper is not part of mvc framework, you should look up for third party library containing that helper.

Uploading file that is part of ViewModel is simple though. Basically it goes like this

Define view model

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

Inside Views/Shared/EditorTemplates, create MyViewModel.cshtml

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

And view, corresponding to upload action

@model MyViewModel

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

required attribute is important to upload files.

And that's it, once form is submitted, you should see uploaded file inside [HttpPost] action, vm.MyFile.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...