我想在动态表单列表中上传多个文件。我的模型就像
class mymodel
{
public IEnumerable<FileAttachmentViewModel> FileAttachments { get; set; }
}
public class FileAttachmentViewModel
{
[DataType(DataType.Upload)]
public IEnumerable<HttpPostedFileBase> files { get; set; }
public bool IsPrivate { get; set; }
public string Description { get; set; }
}
但是我的控制器会为文件
返回null
我的观点
<div class="panel panel-default">
<div class="panel-heading">
File 1
<button type="button" style="margin-bottom:10px;" class='pull-right btn btn-success add-file'><span class="glyphicon glyphicon-plus"></span>Add File </button>
</div>
<div class="panel-body">
<div class="form-group">
<label class="label-control col-md-3"> Select File </label>
<input type="file" class="form-control col-md-4" name="FileAttachments[0][files]" multiple="multiple" />
<label class="label-control col-md-2"> IsPrivate </label>
<input type="checkbox" name="FileAttachments[0][IsPrivate]" class="col-md-3" value="true">
</div>
<div class="form-group">
<label class="label-control col-md-3"> Description </label>
<textarea name="FileAttachments[0][Description]" class="form-control col-md-9"></textarea>
</div>
</div>
</div>
<div id="fileupload"></div>
使用javascript进行动态查看。 和我的控制器
public ActionResut Register(myviewmodel model)
{
if (model.FileAttachments != null)
{
foreach (var att in model.FileAttachments)
{
if (att.files != null)
{
foreach (HttpPostedFileBase file in att.file)
{
var fileName = Path.GetFileName(file.FileName);
attachment = new Attachment();
attachment.AttachmentId = Guid.NewGuid();
attachment.Description = att.Description;
attachment.URL = "~/Content/Heritage/Attachments/" + fileName;
attachment.Type = file.ContentType;
attachment.HeritageId = heritage.HeritageId;
//attachment.OtherType = model.OtherType;
attachment.IsPrivate = att.IsPrivate;
_AttachmentService.Insert(attachment);
file.SaveAs(Path.Combine(Server.MapPath("~/Content/Heritage/Attachments/" + fileName)));
}
}
}
}
}