我在互联网上搜索可能导致此问题但可能没有发现的问题......
当我将IEnumerable<HttpPostedFileBase> FilesUploadedEvent
作为动作参数与ViewModel一起放置时,一切似乎都正常。
[HttpPost]
public ActionResult Create(EventViewModel model, IEnumerable<HttpPostedFileBase> FilesUploadedEvent)
{
if (ModelState.IsValid)
{
...
问题是当我尝试将其放入ViewModel中的对象
时@model ViewModels.Event.EventViewModel
@using (Html.BeginForm("Create", "Event", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true, null, new { @class = "validation-msg" })
@Html.EditorFor(m => m.BasicInfoSection)
...
<input type="submit" value="Add" />
}
删除第二个参数后的控制器:
[HttpPost]
public ActionResult Create(EventViewModel model)
{
if (ModelState.IsValid)
{
...
视图模型:
public class EventViewModel
{
public BasicInfoSection BasicInfoSection { get; set; }
...
我们将参数放在ViewModels对象中:
public class BasicInfoSection : IValidatableObject
{
public string Remarks { get; set; }
public IEnumerable<HttpPostedFileBase> FilesUploadedEvent { get; set; }
...
这是BasicInfoSection的编辑器模板:
@model ViewModels.Event.Parts.BasicInfoSection
<input id="FilesUploadedEvent" name="FilesUploadedEvent" type="file" data-role="upload" multiple="multiple" autocomplete="off">
@(Html.TextAreaFor(m => m.Remarks, new { @class = "form-control", style = "height:200px;" }))
...
此外,如果表单不会在其他某些字段上验证,是否可以在postBack中返回文件?我读它不是出于安全原因。 Ajax验证是唯一的方法吗?
答案 0 :(得分:2)
文件输入的name
属性不正确。因为FilesUploadedEvent
是BasicInfoSection
的属性,所以为了在回发后绑定,输入必须是
<input name="BasicInfoSection.FilesUploadedEvent" type="file" .. />
另请注意textarea
如何使用名称属性name="BasicInfoSection.Remarks"