2017-04-26 16 views
1

私のInvoicePDFが常にnullである理由はわかりません。HttpPostedFileBaseは常にnullを返します

モデル:

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

私もweb.configファイルでmaxRequestLength="65536"を追加しましたし、それが助けにはなりませんでした。

<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> 

コントローラのアクション:

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

私は、メソッドの開始時にブレークポイントを設定し、ev.InvoicePDFは常にnullです。 どこに問題がありますか?

+0

作成アクションを表示できますか? – Usman

+0

確かに、私はファイルで何もしません。最初はブレークポイントを持っていて、投稿されたモデルをチェックしています。何が助けになるのでしょうか。 [アクションの作成](https://pastebin.com/WCScM7Pn) – Cezar

+1

既にフォームがあるので

を削除できます – Usman

答えて

1

フォームタグを削除します。ビューソースを行います。これは動作します:

@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); 
    } 
+0

私はフォームを ''に変更し、完璧に動作しました。 – Cezar

関連する問題