0

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?

3 Answers 3

1

Have you set the enctype to multipart/form-data on your form tag? I cant see it there...

Once you've done that, have a look at this:

Trying to upload a file with ASP.NET MVC

That should cover your issue?

Sign up to request clarification or add additional context in comments.

Comments

1

You should modify your view to include multipart/form-data:

@using(Html.BeginForm("YourAction", "YourController", FormMethod.Post, new { enctype = "multipart/form-data" } )) {
   //....
}

Comments

0

Looks like you are missing on

@using(Html.BeginForm("YourAction", "YourController", FormMethod.Post, new { enctype = "multipart/form-data" } )) { //.... }

Check out- http://hozefam.com/post/2012/08/10/Exploring-File-Upload-and-Download-in-MVC-3.aspx

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.