将文件绑定到模型时出错
Error binding files to model
我在将文件输入正确绑定到模型 post 时遇到问题。
我正在尝试绑定到下面的模型。
使用 FileWrapper class 的原因,它实际上是字典的替代品,只是为了尝试一些不同于字典的东西,因为我也无法让它工作。如果可以使用字典,我很乐意将其改回原样。
我需要的是能够将多个多个文件输入绑定到不同的文件列表。因此,为什么 FileWrapper 有一个 IENumerable<HttpPostedFileBase>
。将 FileWrappers 放在列表中的原因是因为文件输入的数量可能会有所不同。视图是动态生成的。
当我尝试这样绑定时,索引看起来绑定正确,但文件列表是空的。
如果我可以提供额外的东西,请告诉我。
表单输入如下所示:
Html
<input type="hidden" name="FileWrappers[0].Index" value="0" />
<input type="file" name="FileWrappers[0].Files" multiple>
型号
public class SpecialBeregningModel
{
public List<FileWrapper> FileWrappers { get; set; }
public SpecialBeregningModel()
{
FileWrappers = new List<FileWrapper>();
}
}
public class FileWrapper
{
public int Index { get; set; }
public IEnumerable<HttpPostedFile> Files { get; set; }
public FileWrapper()
{
Files = new List<HttpPostedFile>();
}
}
你的 属性 应该是 HttpPostedFileBase
,而不是 HttpPostedFile
:
public IEnumerable<HttpPostedFileBase> Files { get; set; }
也不要忘记包含 <form>
.
的 enctype="multipart/form-data"
属性
我在将文件输入正确绑定到模型 post 时遇到问题。 我正在尝试绑定到下面的模型。
使用 FileWrapper class 的原因,它实际上是字典的替代品,只是为了尝试一些不同于字典的东西,因为我也无法让它工作。如果可以使用字典,我很乐意将其改回原样。
我需要的是能够将多个多个文件输入绑定到不同的文件列表。因此,为什么 FileWrapper 有一个 IENumerable<HttpPostedFileBase>
。将 FileWrappers 放在列表中的原因是因为文件输入的数量可能会有所不同。视图是动态生成的。
当我尝试这样绑定时,索引看起来绑定正确,但文件列表是空的。
如果我可以提供额外的东西,请告诉我。
表单输入如下所示:
Html
<input type="hidden" name="FileWrappers[0].Index" value="0" />
<input type="file" name="FileWrappers[0].Files" multiple>
型号
public class SpecialBeregningModel
{
public List<FileWrapper> FileWrappers { get; set; }
public SpecialBeregningModel()
{
FileWrappers = new List<FileWrapper>();
}
}
public class FileWrapper
{
public int Index { get; set; }
public IEnumerable<HttpPostedFile> Files { get; set; }
public FileWrapper()
{
Files = new List<HttpPostedFile>();
}
}
你的 属性 应该是 HttpPostedFileBase
,而不是 HttpPostedFile
:
public IEnumerable<HttpPostedFileBase> Files { get; set; }
也不要忘记包含 <form>
.
enctype="multipart/form-data"
属性