Cezar
Cezar

Reputation: 355

HttpPostedFileBase always return null

I don't know why my InvoicePDF is always null.

Model:

public class CreateEventViewModel
{
    // [FileExtensions(Extensions = "pdf", ErrorMessage = "Akceptuję tylko pliki PDF")]
    public HttpPostedFileBase InvoicePDF { get; set; }
    ...
}

I've also added maxRequestLength="65536" in web.config and it didn't help.

<form id="f">
    @using (Html.BeginForm("Create", "Event", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()
        <div>
            @Html.TextBoxFor(model => model.InvoicePDF, new { type = "file" })
            @*@Html.ValidationMessageFor(model => model.InvoicePDF)*@
        </div>
        ...
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <button type="button" class="btn btn-default" onclick="CountPrice()">Oblicz cenę</button>
                    <input type="submit" value="Zapisz" class="btn btn-default" />
                    <button type="button" id="cancel" class="btn btn-default">Wyjdź</button>
                </div>
            </div>
        </div>
    }
</form>

Controller action:

[HttpPost]
public async Task<ActionResult> Create(CreateEventViewModel ev)
{
  ...
}

I set breakpoint at the method start and ev.InvoicePDF is always null. Where is the problem?

Upvotes: 1

Views: 436

Answers (1)

kblau
kblau

Reputation: 2107

Remove your form tag. Do a view source. This will work:

@model Testy20161006.Controllers.CreateEventViewModel
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index53</title>
</head>
<body>
    <div>
        @*  remove this <form id="f">*@  
            @using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
            {
                @Html.AntiForgeryToken()
                <div>
                    @Html.TextBoxFor(model => model.InvoicePDF, new { type = "file" })
                    @*@Html.ValidationMessageFor(model => model.InvoicePDF)*@
                </div>

                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <button type="button" class="btn btn-default" onclick="CountPrice()">Oblicz cenę</button>
                        <input type="submit" value="Zapisz" class="btn btn-default" />
                        <button type="button" id="cancel" class="btn btn-default">Wyjdź</button>
                    </div>
                </div>
            }
        @*</form>*@
    </div>
</body>
</html>

public class HomeController : Controller
{
    [HttpPost]
    public async Task<ActionResult> Create(CreateEventViewModel ev)
    {
        return View();
    }

    public ActionResult Index53()
    {
        CreateEventViewModel createEventViewModel = new CreateEventViewModel();
        return View(createEventViewModel);
    }

Upvotes: 2

Related Questions