I need an action to create MyModel type with some fields and also some files to upload to DB. So this is a part of my View:
@using(Html.BeginForm()) {
<fieldset>
<dl>
<dt>
@Html.LabelFor(model => model.Name)
</dt>
<dd>
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</dd>
<dt>
@Html.LabelFor(model => model.NeededFiles)
</dt>
<dd>
@foreach(var item in Model.NeededFiles) {
<div style="margin:5px;">
@Html.Label(item.Title)
<input type="file" name="@item.Name" />
</div>
}
</dl>
</fieldset>
<input type="submit" value="Create" class="button red" />
}
But in post Action I have some problem to get file Path: This is my Action:
[HttpPost]
public ActionResult Create(MyModel ViewModel FormCollection collection, IEnumerable<HttpPostedFileBase> files) {
//....
}
Request.Files is null so I can't use it and I don't know why. also files IEnumerable<HttpPostedFileBase> is null. then I try to get from FormCollection
so I use this:
string filePath = collection.GetValue(item.Name).AttemptedValue;
but filePath just returned file Name not File path, I don't understand. when I choose file I can see all path in input but when I want to get it just file Name returned. where is my problem? what is your suggestion?