2012-01-25 6 views
0

私はテキストボックスの動的リストを作成しています。ユーザーがフィールドに値を送信するとnullが返されます。私は何かが足りないと思う。MVC3テキストボックスはnull値を送信しますか?

これは私の製品モデルである:

public class EnqProduct 
{ 
    public string Id { get; set; } 
    public string Product { get; set; } 
    public string Quantity { get; set; } 
} 

これは、上記のリストを含むページモデルです。

public IList<EnqProduct> EnqProduct { get; set; } 

これは私がモデルを設定しています方法です:

IList<EnqProduct> items2 = Session["enquiry"] as IList<EnqProduct>; 
var EnquiryModel = new Enquiry { 
     EnqProduct = items2 
};  
return View(EnquiryModel); 

、これは私がフィールドに表示する方法です:ユーザーはフィールドがコントローラに戻る行く送信すると

foreach (var item in Model.EnqProduct) 
{ 
<tr> 
    <td> 
     <span class="editor-field"> 
     @Html.TextBox(item.Id, item.Product) 
     @Html.ValidationMessageFor(m => m.A1_Enquiry) 
     </span> 
     <br><br>        
    </td> 
    <td> 
     <span id = "field" class="editor-field"> 
     @Html.TextBox(item.Id, item.Quantity) 
     </span>  
     <br><br>        
    </td> 
    </tr> 
} 

をヌル?

答えて

2

私は、エディタのテンプレートを使用してことをお勧めしますし、次を使用して、foreachループを交換します:

@model Enquiry 
<table> 
    <thead> 
     <tr> 
      <th>product name</th> 
      <th>quantity</th> 
     </tr> 
    </thead> 
    <tbody> 
     @Html.EditorFor(x => x.EnqProduct) 
    </tbody> 
</table> 

し、自動的EnqProductコレクション(~/Views/Shared/EditorTemplates/EnqProduct.cshtml)の各要素に対してレンダリングされるエディタのテンプレートを定義します。

@model EnqProduct 
<tr> 
    <td> 
     @* We include the id of the current item as hidden field *@ 
     @Html.HiddenFor(x => x.Id) 

     <span class="editor-field"> 
      @Html.EditorFor(x => x.Product) 
      @Html.ValidationMessageFor(x => x.Product) 
     </span> 
    </td> 
    <td> 
     <span id="field" class="editor-field"> 
      @Html.EditorFor(x => x.Quantity) 
     </span>  
    </td> 
</tr> 

あなたは正しい値を取得するフォームを送信:

public class HomeController: Controller 
{ 
    public ActionResult Index() 
    { 
     var model = new Enquiry(); 
     model.EnqProduct = ... 
     return View(model); 
    } 

    [HttpPost] 
    public ActionResult Index(Enquiry model) 
    { 
     // the model.EnqProduct will be correctly populated here 
     ... 
    } 
} 

デフォルトのモデルバインダーが入力フィールドに必要とする正しいワイヤー形式の場合は、following articleをご覧ください。一部のモデルが正しく実装されていない場合、機能の問題をより簡単にデバッグすることができます。 FireBugとPOSTされている値の名前を調べるだけで十分です。あなたはそれらがOKかKOかどうかをすぐに知ることができます。

+0

私は感謝しました! – Beginner

関連する問題