2017-11-24 10 views
0

をコントローラへのビューの要素のリストを送信します材料を表す。テーブルは次のようになります。私が欲しいは、私はこのモデルを持っている

enter image description here

、例えば、テーブルの最初の2行を埋めるために、コントローラに値を送信します。

私はこれをやろうとしましたが、材料リストはコントローラ内でヌルです。

@model CarListViewModel 

@{ 
    ViewData["Title"] = "Registar material"; 
} 

<div class="material-registration-wrapper"> 
    <h2 class="title">Registo materiais - Carro</h2> 

    @using (Html.BeginForm("MaterialDummy", "Material", FormMethod.Post, new { id = "materialRegistrationForm" })) 
    { 
     <table class="material-registration-insertion"> 
      <tr> 
       <th>Material</th> 
       <th>Lote</th> 
       <th>Quantidade</th> 
      </tr> 
      <tr> 
       @{List<Material> mList = new List<Material>();} 
       @{Material m1 = new Material();} 
       <td>@Html.TextBoxFor(model => m1.name, null, new { type = "text" })</td> 
       <td>@Html.TextBoxFor(model => m1.lot, null, new { type = "text" })</td> 
       <td>@Html.TextBoxFor(model => m1.quantity, null, new { type = "text" })</td> 
      </tr> 
      <tr> 
       @{Material m2 = new Material();} 
       <td>@Html.TextBoxFor(model => m2.name, null, new { type = "text" })</td> 
       <td>@Html.TextBoxFor(model => m2.lot, null, new { type = "text" })</td> 
       <td>@Html.TextBoxFor(model => m2.quantity, null, new { type = "text" })</td> 
      </tr> 
      <tr> 
       @{Material m3 = new Material();} 
       <td>@Html.TextBoxFor(model => m3.name, null, new { type = "text" })</td> 
       <td>@Html.TextBoxFor(model => m3.lot, null, new { type = "text" })</td> 
       <td>@Html.TextBoxFor(model => m3.quantity, null, new { type = "text" })</td> 
      </tr> 
      @{ mList.Add(m1); } 
      @{ mList.Add(m2); } 
      @{ mList.Add(m3); } 
      @Html.HiddenFor(model => mList, Model.materials) 
     </table> 
     <table class="submit-table"> 
      <tr> 
       <td> 
        <div class="submit-table"> 
         <input type="submit" value="Submeter" class="submit-button" />--> 
        </div> 
       </td> 
      </tr> 
     </table> 
    } 
</div> 

私が間違ってやっていること、またはこれを行うためのより良い方法は誰にも分かりますか?

+1

MVCのリストのModelBindingに関する情報がたくさんあります。これを見てみましょう:https://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/ –

+1

コレクションに隠された要素を保持することに意味はありません。入力名が正しい限り、モデルバインダーはそれをパラメーターにマップできます。ですから、 'Materials [0] .name'、' Materials [1] .name'という形式で名前をつけて入力要素を構築してください。 UIのMaterialsコレクションの行をどのように作成していますか? – Shyju

+0

あなたはビューの意味ですか?私はいくつかの行といくつかの列を持つHTMLテーブルを持っています。私はその質問にコードを掲載した。 – undisp

答えて

0

Daniel StackenlandとShyjuの助けを借りて、私はそれを機能させることができました。私は必要な唯一のものは、直接そのようなCarListViewModelクラスの材料リストに値を追加することでした

:送信ボタンを押すと

<table class="material-registration-insertion"> 
    <tr> 
     <th>Material</th> 
     <th>Lote</th> 
     <th>Quantidade</th> 
    </tr> 
    <tr> 
     <td>@Html.TextBoxFor(model => model.materials[0].name, null, new { type = "text" })</td> 
     <td>@Html.TextBoxFor(model => model.materials[0].lot, null, new { type = "text" })</td> 
     <td>@Html.TextBoxFor(model => model.materials[0].quantity, null, new { type = "text" })</td> 
    </tr> 
    <tr> 
     <td>@Html.TextBoxFor(model => model.materials[1].name, null, new { type = "text" })</td> 
     <td>@Html.TextBoxFor(model => model.materials[1].lot, null, new { type = "text" })</td> 
     <td>@Html.TextBoxFor(model => model.materials[1].quantity, null, new { type = "text" })</td> 
    </tr> 
    <tr> 
     <td>@Html.TextBoxFor(model => model.materials[2].name, null, new { type = "text" })</td> 
     <td>@Html.TextBoxFor(model => model.materials[2].lot, null, new { type = "text" })</td> 
     <td>@Html.TextBoxFor(model => model.materials[2].quantity, null, new { type = "text" })</td> 
    </tr> 
</table> 

、値が自動的にコントローラに送信されます。

+1

は私がコメントで言及したようです。必ずしもヘルパーメソッドを使う必要はありません。入力要素に純粋なhtmlコードを書くことができます。 'name'属性の値が適切であることを確認するために必要なのは、モデルバインダーが投稿されたフォームデータをマップできるようにするためだけです – Shyju

関連する問題