7

I have the following model

public class VacancyDetailModel
{
    public HttpPostedFileBase Resume {get;set; }
}

This is my form.

@using (Html.BeginForm())
{
  <input type="file" name="Resume" />
  <input type="hidden" name="fhController" value="VacanciesController" />
  <input type="hidden" name="fhAction" value="VacancyDetail" />
  <button id="btn-save" class="btn btn-highlight" type="submit">Solliciteer direct empty form</button>
}

This is my controller

[HttpPost]     
//[ValidateFormHandler]       
public ActionResult VacancyDetail(VacancyDetailModel formModel)
{
    return this.View(model);
}

When I place a watch on my model, the Resume field is always null.

1
  • 1
    Can you share the controller code as well, please. In the VacanciesController where you encounter NULL?
    – Mark Cassidy
    Commented Oct 31, 2016 at 10:57

2 Answers 2

7

You need to define your form properly for file uploads to work. The most important part is to set enctype to multipart/form-data. Normally, you should use the standard Html.BeginForm helper:

@using (Html.BeginForm("VacancyDetail", "VacanciesController", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  <input type="file" name="Resume" />
  <button id="btn-save" class="btn btn-highlight" type="submit">Solliciteer direct empty form</button>
}

If you, for some reason, can't use a fully parameterized Html.BeginForm, use raw HTML instead:

<form action="" method="post" enctype="multipart/form-data">
  <input type="file" name="Resume" />
  <input type="hidden" name="fhController" value="VacanciesController" />
  <input type="hidden" name="fhAction" value="VacancyDetail" />
  <button id="btn-save" class="btn btn-highlight" type="submit">Solliciteer direct empty form</button>
</form>

You can find more examples on the Haacked blog.

2
  • I can't use Html.BeginForm("VacancyDetail", "VacanciesController" in Sitecore according to my other question I asked here. So I need a empty beginform()
    – Danny
    Commented Oct 31, 2016 at 12:16
  • @DannyH I have edited my answer according to your request. Commented Oct 31, 2016 at 12:22
2

You need to specify an enctype of multipart/form-data if your form uses file input types.

You can either do this manually:

<form action="" enctype="multipart/form-data" method="POST">

Or by using the HtmlHelper, but you would need to use the particular overload that allows you to specify HTML attributes:

@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new {enctype = "multipart/form-data" })) {
2
  • I can't use Html.BeginForm("VacancyDetail", "VacanciesController" in SiteCore according to my other question I asked here. So I need a empty beginform()
    – Danny
    Commented Oct 31, 2016 at 12:16
  • You can use the manual form entry to achieve the same as BeginForm(), alternatively you could also look at the source for BeginForm so that you can create your own extension that allows you to specify the HTML attributes but no controller / action.
    – Kasaku
    Commented Oct 31, 2016 at 12:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.