File upload bound to the Viewmodel

无人久伴 提交于 2019-11-29 04:22:10

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.

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..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!