Sitecore Stack Exchange is a question and answer site for developers and end users of the Sitecore CMS and multichannel marketing software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
1  
Can you share the controller code as well, please. In the VacanciesController where you encounter NULL? – Mark Cassidy 2 days ago
up vote 6 down vote accepted

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.

share|improve this answer
    
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 H 2 days ago
    
@DannyH I have edited my answer according to your request. – Dmytro Shevchenko 2 days ago

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" })) {
share|improve this answer
    
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 H 2 days ago
    
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 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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