0

For a project i've been trying to upload a file from my view to my controller action. However, for some reason the action is reached but for the file i receive a null.

Here is my code:

Action:

[HttpPost]
[Route("UploadSickNote")]
public ActionResult UploadSickNote(HttpPostedFileBase file)
{
    // Do something with file

    return RedirectToAction("SickNotes");
}

View:

<form Controller="Documents" Action="UploadSickNote" method="post">
    <div>
        <input id="sicknote-upload" name="UploadedSickNote" asp-for="UploadedSickNote" type="file" accept="image/*" capture>
        <input id="sicknote-button-submit" type="submit"/>
    </div>
</form>

I have also tried using a viewmodel but with the same results.

Action:

[HttpPost]
[Route("UploadSickNote")]
public ActionResult UploadSickNote(SickNotesViewModel vm)
{
    // Do something with file

    return RedirectToAction("SickNotes");
}

ViewModel:

public class SickNotesViewModel
{
    public List<SickNoteElement> SickNoteElements { get; set; }
    public HttpPostedFileBase UploadedSickNote { get; set; }
}

I am using asp.net mvc 5 (not asp.net core) and a bit of vue.js for view functionalities. Any ideas or tips are greatly appreciated.

2 Answers 2

2

Edit:

You have to make sure that your input name matches your controller so

<input id="sicknote-upload" name="file" asp-for="UploadedSickNote" type="file" accept="image/*" capture>

if with that doesnt work you could try also specifying the type of form you are sending

<form Controller="Documents" Action="UploadSickNote" method="post" enctype="multipart/form-data">
Sign up to request clarification or add additional context in comments.

5 Comments

Fromform is for asp.net core. Which i am not allowed to use at the moment.My other actions that receive viewmodels all work without the use for fromform.
My bad i ll edit accordingly, you will have to change some things in your View then
The 2nd code fragment also works, i'll accept it as answer since it's prettier than what i found, thanks
Technically speaking they re the same, @Html.BeginForm is just a wrapper for the form tag, if you inspect the code you will see the same result
Yes but i do not like the look of the html wrappers in code so it's just personal preference :)
0

Found the solution after some more searching. I now receive the file in my viewmodel by changing my view to this:

   @using (Html.BeginForm("UploadSickNote", "Documents", FormMethod.Post, new { enctype = "multipart/form-data" }))
   {
            <div>
                <input id="sicknote-upload" name="UploadedSickNote" type="file" accept="image/*" capture>
                <input id="sicknote-button-submit" type="submit"/>
            </div>
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.