我有一个模型(简化,删除无关属性):
public class SubmitModel
{
[Required]
[DataType("FileUpload")]
[Display(Name = "Formatted Data File")]
public HttpPostedFileBase FormattedDataFile { get; set; }
}
控制器:
[HttpPost]
public ActionResult Submit(SubmitModel model)
{
if (this.ModelState.IsValid)
{
//...
}
return this.View(model);
}
文件上传视图:
@{
IDictionary<string, object> htmlAttributes = Html.GetUnobtrusiveValidationAttributes(string.Empty);
}
<input type="file" id="@this.ViewData.TemplateInfo.GetFullHtmlFieldId(string.Empty)" name="@this.ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)" @(new MvcHtmlString(htmlAttributes.ToHtmlAttributesString())) />
@Html.ValidationMessage(string.Empty)
和一个简单的观点:
@model SubmitModel
@using (Html.BeginForm())
{
<div class="Form">
@Html.EditorForModel()
<div class="Footer">
<button class="Button" data-options='{ "icons": { "primary": "ui-icon-disk" } }'>Submit</button>
</div>
</div>
}
呈现为此 HTML:
<form action="/Data/Submit" method="post">
<div class="Form">
<div class="Item">
<div class="Label Required">Formatted Data File:</div>
<div class="Input">
<input type="file" id="FormattedDataFile" name="FormattedDataFile" data-val-required="The Formatted Data File field is required." data-val="true" />
<span class="field-validation-error" data-valmsg-for="FormattedDataFile" data-valmsg-replace="true">The value 'Test.xlsx' is invalid.</span>
</div>
</div>
<div class="Footer">
<button class="Button" data-options='{ "icons": { "primary": "ui-icon-disk" } }'>Submit</button>
</div>
</div>
</form>
单击 后Submit
,我被带到正确的控制器/操作,并且我的模型的FormattedDataFile
属性为空。ModelState
无效,说“需要格式化数据文件字段” 。同样的代码在我完成的一些 MVC-3 项目中运行良好——在 MVC-4 中这有什么不同吗?