24

When I hit submit, the file parameter is null.

public ActionResult Create()
{
  return View(new FileViewModel());
}

[HttpPost]    
[InitializeBlobHelper]
public ActionResult Create(FileViewModel file)
{
  if (ModelState.IsValid)
  {
     //upload file
  }
  else
    return View(file);
}

public class FileViewModel
{
  internal const string UploadingUserNameKey = "UserName";
  internal const string FileNameKey = "FileName";

  internal const string Folder = "files";

  private readonly Guid guid = Guid.NewGuid();

  public string FileName
  {
    get
    {
      if (File == null)
        return null;
      var folder = Folder;
      return string.Format("{0}/{1}{2}", folder, guid, Path.GetExtension(File.FileName)).ToLowerInvariant();
    }
  }

  [RequiredValue]
  public HttpPostedFileBase File { get; set; }
}

Here is the cshtml:

@model MyProject.Controllers.Admin.FileViewModel

@{
  ViewBag.Title = "Create";
  Layout = "~/Views/Shared/_BackOfficeLayout.cshtml";
}

@using (Html.BeginForm("Create", "Files", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  <fieldset>
    <legend>Create</legend>

    <div class="editor-label">
      @Html.LabelFor(model => model.File)
    </div>
    <div class="editor-field">
      @Html.TextBoxFor(model => model.File, new { type = "file" })
      @Html.ValidationMessageFor(model => model.File)
    </div>

    <p>
      <input type="submit" value="Create" />
    </p>
  </fieldset>
}

<div>
  @Html.ActionLink("Back to List", "Index")
</div>

2 Answers 2

73

It's naming conflict and binder trying to bind your File property to FileViewModel object with file name, that's why you get null. POST names are case-insensitive.

Change:

public ActionResult Create(FileViewModel file)

To:

public ActionResult Create(FileViewModel model)

or to any other name

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

7 Comments

Shiat! I didn't believe it's gonna work but it did. Why indeed did this happen?
@Shimmy It's naming conflict and binder trying to bind your File property to FileViewModel object with file name, that's why you get null. POST names are case-insensitive.
i am getting sort of same error.but i dont get your solution
thanks, was stuck in hours could not figure why it stopped working suddenly
This is particularly insidious when you go back to modify older code written by someone else. I never use "model" by itself as an object name and added a "Model" field to a nested object containing vehicle information, so code that worked for 7 years suddenly broke for no immediately obvious reason.
|
3

This solved my issue as well. It was a name that I was using that was similar to the model, which was similar to the variable I assigned the posted model too. once I sorted out the field name all worked as expected.

Of course the error was not helpful in pointing this out.

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.