Reputation: 27773
I have a model (simplified, removing extraneous properties):
public class SubmitModel
{
[Required]
[DataType("FileUpload")]
[Display(Name = "Formatted Data File")]
public HttpPostedFileBase FormattedDataFile { get; set; }
}
A controller:
[HttpPost]
public ActionResult Submit(SubmitModel model)
{
if (this.ModelState.IsValid)
{
//...
}
return this.View(model);
}
A FileUpload view:
@{
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)
And a simple view:
@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>
}
Which renders to this 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>
Upon clicking Submit
, I'm brought to the proper controller/action and my model's FormattedDataFile
property is null. The ModelState
is invalid, saying that "The Formatted Data File field is required." This same code worked fine in some MVC-3 projects I've done - is there anything different regarding this in MVC-4?
Upvotes: 1
Views: 591
Reputation: 4624
i think you are missing enctype="multipart/form-data"
in the form
http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2
Upvotes: 1