2017-01-29 4 views
-1

私はASP.NET MVCを勉強しています。5.ビュー "Create"を作成しました。しかし、私はRazorを使って入力フィールドを生成しているわけではありません。純粋なhtmlで入力を使用しています。HttpPostの後の塗りつぶしのフィールドCreate - オブジェクトは入力を使用してnullです

Create.cshtml

@model MyProject.Product 

<h2>Create Product</h2> 

<form method="post"> 

    Description: <br /> 
    <input type="text" name="Description" id="Description"/> <br /> 
    ValueType: <br /> 
    <input type="text" name="ValueType" id="ValueType"/> 
    <br /> 
    <input type="submit" name="btSend"/> 

</form> 

マイコントローラー:

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

     [HttpPost] 
     public ActionResult Create(Product product) 
     { 
      if (ModelState.IsValid) 
      {     
       db.Product.Add(product);     
       db.SaveChanges();     
       return RedirectToAction("Index"); 

      } 
      else 
      { 
       return View(product); 
      } 

    It works fine. I can create new products. 

しかし、私はモデルで注釈をいくつかのサーバー側の検証を使用する必要があります。 したがって、データを送信したいと思います。モデルが有効でない場合は、値を使用してCreateに戻ります。私は検証メッセージを入れる方法を知っています。だから、私はこれを試しました:

@model MyProject.Product 

<h2>Create Product</h2> 

<form method="post"> 

    Description: <br /> 
    <input type="text" name="Description" id="Description" value="@Model.Description"/> <br /> 
    ValueType: <br /> 
    <input type="text" name="ValueType" id="ValueType" value="@Model.ValueType"/> 
    <br /> 
    <input type="submit" name="btSend"/> 

</form> 

純粋な入力をモデル化するにはどうすればいいですか?

なぜNULL値ですか。

ありがとうございます。

+1

なぜあなたは剃刀を使用していませんか? '@Html.LabelFor()'、 '@Html.TextBoxFor()'、 '@ Html.ValidationMessageFor()'を使ってフォームコントロールを正しく生成し、生成したすべてのHTMLを見て、 2ウェイモデルバインドと検証なし –

+0

私はhtmlが作成されたフロントエンド開発者と協力して作業するためです。私は、入力型で作業する方法があるかどうかを知りたい。どのようにそれをバインドするには? – ComplexityAlg

+0

'HtmlHelper'メソッドを使うと、双方向モデルのバインディングと検証に必要な正しいhtmlが得られます。そして '@ Model.Description'は剃刀コードなので、あなたが剃刀を使用したくないという要望はありません –

答えて

0

razor based form approachを使用しない場合は、Viewbag/ViewDataの表示確認メッセージを使用できます。

[HttpPost] 
    public ActionResult Create(Product product) 
    { 
     if (!ModelState.IsValid) 
     { 
      //if you want to get validation message from ModelState itself, you can query from Modelstate : 
      string message = string.Join(" , ", ModelState.Values 
           .SelectMany(v => v.Errors) 
           .Select(e => e.ErrorMessage)); 
      ViewData["ValidationMessage"] = "Validation Message";// you can use above variable message here 
      return View(product); 
     } 
    // your other implementation 
    } 

あなたのビューは次のようにする必要があります:私はあなたがそうすることを許可されている場合razor based form approachを使用することをお勧めします、

@model MyProject.Product 

<h2>Create Product</h2> 

<form method="post"> 
    <div class="error-message">@ViewData["ValidationMessage"]</div> 
    Description: <br /> 
    <input type="text" name="Description" id="Description" value="@Model.Description"/> <br /> 
    ValueType: <br /> 
    <input type="text" name="ValueType" id="ValueType" value="@Model.ValueType"/> 
    <br /> 
    <input type="submit" name="btSend"/> 

</form> 

しかし。

+0

解決策**は剃刀を使用しています(これは' @ViewData ["ValidationMessage"] ' –

+0

ええ、しかし、OPはすでに "@ Model.Description"や "@ Model.ValueType"のようなシンプルなカミソリを使っています。 –

+0

はい、私は知っています(質問にコメントしたように)。 razor_を使用したいが、次にカミソリを使用してソリューションを表示する:) –

0

私はあなたが剃刀のビューエンジンを誤解するかもしれないと思います。ページ2.0と3.0(かみそり)にはデータバインディングがありません。それはhtmlヘルパーでそれをエミュレートしますが、これはネイティブの剃刀データバインディングではありません。ヘルパー(https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Html/DefaultEditorTemplates.cs)の裏にあるコードを見て、あなたが望んでいる作業を実行していることがわかります。

関連する問題