2013-08-14 2 views
8

これは次のフォームを送信するとコントローラが自分のモデルを取得できないという問題です。 BillingCode(BillingCodeObjectsのリスト)のアイテムをループスルーして表示しようとしています。これらのコードを削除して、状況に実際には関係なく、読みやすくすることができます。ここでMVC 4モデルでフォームを送信するとコントローラーポストのメソッドがnullになる

@using (Html.BeginForm("SubmitTimesheet", "Timesheet", FormMethod.Post)) 
{ 


foreach (var item in Model.BillingCodes) 
{ 

    <div class="button-padding"> 
     <a class="btn btn-large btn-danger btn-block billCodeBtn"> 
      <div class="btnText">@item.Name</div> 
      <div class="btnTime">@item.TotalHours</div> 

      <i class="icon-chevron-down billCodeIconUp billCodeIcon"></i> 
      <i class="hidden icon-chevron-up billCodeIconDown billCodeIcon"></i> 
     </a> 

     <div class="content" > 
      <div class="row timeEntry"> 
       <p></p> 
       <div class="col-12 col-lg-2"> 
        Enter Time: 
     <div class="row"> 
      <div class="col-12"> 
       @Html.DropDownListFor(model => item.EnterTimeHours, new SelectList(new[] { 
        new { Value = "0", Text = "0" }, 
        new { Value = "1", Text = "1" }, 
        new { Value = "2", Text = "2" }, 
        new { Value = "3", Text = "3" }, 
        new { Value = "4", Text = "4" }, 
        new { Value = "5", Text = "5" }, 
        new { Value = "6", Text = "6" }, 
        new { Value = "7", Text = "7" }, 
        new { Value = "8", Text = "8" }, 
        new { Value = "9", Text = "9" }, 
        new { Value = "10", Text = "10" }, 
        new { Value = "11", Text = "11" }, 
        new { Value = "12", Text = "12" }, 
       }, "Value", "Text")) <b>:</b> 
       @Html.DropDownListFor(model => item.EnterTimeMinutes, new SelectList(new[] { 
        new { Value = "0", Text = "00" }, 
        new { Value = "15", Text = "15" }, 
        new { Value = "30", Text = "30" }, 
        new { Value = "45", Text = "45" }, 
       }, "Value", "Text")) 

      </div> 
     </div> 
       </div> 
       <div class="col-lg-2"></div> 

       <div class="col-lg-1"></div> 
       <div class="control-group col-12 col-lg-2"> 
        <label class="checkbox"> 
         Billable @Html.CheckBoxFor(model => item.Billable) 
        </label> 
       </div> 
       <div class="col-12 col-lg-2"> 
        Enter Memo: 
     <div class="row"> 
      <div class="col-12"> 
       @Html.TextAreaFor(model => item.Comment) 
      </div> 
     </div> 

そして、ここでは私のコントローラのためのいくつかのコードです...私のビューのためのコードです:

public class TimesheetController : Controller 
{ 
    // 
    // GET: /Timesheet/ 

    public ActionResult Index() 
    { 

     string getBillingCodeUrl =""; 

     //SOME CODE REMOVED FOR LENGTH/READABILITY 

     foreach (var entryItem in timePeriod.TimeEntries[0].EntryCollection) 
     { 
      foreach (var billingItem in billingCodeList.BillingCodes) 
      { 
       if (entryItem.BillingCode == billingItem.Name) 
       { 
        //update record in billingItem with data from entryItem 
        billingItem.Comment = entryItem.Comment; 
        billingItem.Billable = entryItem.Billable; 
        billingItem.TotalHours = entryItem.Hours; 
       } 
      } 
     } 

     return View(billingCodeList); 
    } 
    [HttpPost] 
    public void SubmitTimesheet(BillingCodeList model) 
    { 

     string uri = ""; 

     foreach (var billingCode in model.BillingCodes) 
     { 
      //do stuff with each of these 
     } 
    } 

} 
} 

そして最後に、こちらのモデルである情報は以下のとおりです。フォームが返されたとき、ここで

public class BillingCodeList 
    { 
     public List<BillingCodeObj> BillingCodes; 
    } 

    public class BillingCodeObj 
    { 
     public string Name { get; set; } 
     public decimal TotalHours { get; set; } 

     public decimal EnterTimeHours { get; set; } 
     public decimal EnterTimeMinutes { get; set; } 
     public bool Billable { get; set; } 
     public string Comment { get; set; } 

     public BillingCodeObj(string name, decimal hours) 
     { 
      this.Name = name; 
      this.TotalHours = hours; 
     } 
     public BillingCodeObj() 
     { 

     } 
    } 

は、デバッグ地元の人々の絵..です

image of locals

答えて

6

入力要素がこれに似どこかに掲示値を持っている必要がありますので、あなたは、ビューのforeachのをやっている: name="BillingCodes[0].EnterTimeHours"name="BillingCodes[0].EnterTimeMinutes"あなたはネットワーク]タブでChromeデベロッパーツールの要求(CTRL +シフト+ Cを検査することができます) またはソースを表示するだけです。この場合、複数のBillingCodeObjオブジェクトを送信しています。それを受け取るコントローラーが必要です。

舞台裏で何が起こっているのかを理解するのに役立つので、ソースコードを見てください。

はあなたのコントローラでこれを試してみてください:

[HttpPost] 
public void SubmitTimesheet(IEnumerable<BillingCodeObj> billingCodes){ 
} 

あなたはまた(デバッグ目的のために)

public void SubmitTimesheet(FormCollection form){} 

を行うと、デバッグに移入されどのようにフォームを検査することができます。

コメントやに表示を変更 提供より多くのコードの後:

@model BillingCodeObj 
<div class="button-padding"> 
    <a class="btn btn-large btn-danger btn-block billCodeBtn"> 
     <div class="btnText">@Model.Name</div> 
     <div class="btnTime">@Model.TotalHours</div> 

     <i class="icon-chevron-down billCodeIconUp billCodeIcon"></i> 
     <i class="hidden icon-chevron-up billCodeIconDown billCodeIcon"></i> 
    </a> 

    <div class="content" > 
     <div class="row timeEntry"> 
      <p></p> 
      <div class="col-12 col-lg-2"> 
       Enter Time: 
    <div class="row"> 
     <div class="col-12"> 
      @Html.DropDownListFor(model => model.EnterTimeHours, new SelectList(new[] { 
       new { Value = "0", Text = "0" }, 
       new { Value = "1", Text = "1" }, 
       new { Value = "2", Text = "2" }, 
       new { Value = "3", Text = "3" }, 
       new { Value = "4", Text = "4" }, 
       new { Value = "5", Text = "5" }, 
       new { Value = "6", Text = "6" }, 
       new { Value = "7", Text = "7" }, 
       new { Value = "8", Text = "8" }, 
       new { Value = "9", Text = "9" }, 
       new { Value = "10", Text = "10" }, 
       new { Value = "11", Text = "11" }, 
       new { Value = "12", Text = "12" }, 
      }, "Value", "Text")) <b>:</b> 
      @Html.DropDownListFor(model => model.EnterTimeMinutes, new SelectList(new[] { 
       new { Value = "0", Text = "00" }, 
       new { Value = "15", Text = "15" }, 
       new { Value = "30", Text = "30" }, 
       new { Value = "45", Text = "45" }, 
      }, "Value", "Text")) 

     </div> 
    </div> 
      </div> 
      <div class="col-lg-2"></div> 

      <div class="col-lg-1"></div> 
      <div class="control-group col-12 col-lg-2"> 
       <label class="checkbox"> 
        Billable @Html.CheckBoxFor(model => model.Billable) 
       </label> 
      </div> 
      <div class="col-12 col-lg-2"> 
       Enter Memo: 
    <div class="row"> 
     <div class="col-12"> 
      @Html.TextAreaFor(model => model.Comment) 
     </div> 
    </div> 
+0

さて、ありがとう。最初のことはうまくいかず、billingCodesオブジェクトはまだnullです。だから私はしばらく時間をとり、それが返す形式を調べるでしょう。 –

+0

あなたがhtmlに生成された入力を投稿することができれば、私は喜んであなたを助けようとします。 –

+0

大丈夫私のオリジナルの記事を編集し、地元の人の写真もここに入れました:[リンク](http://i.imgur.com/0KXBDXr.png) –

0

あなたのビューはBillingCodeListのタイプですが、コントローラ内で使用するアクションにList<BillingCodeObj>を送信しようとしています。

てみてください、次のようにアクションを変更するには:

[HttpPost] 
    public void SubmitTimesheet(BillingCodeList model) 
    { 

     string uri = ""; 

     foreach (var billingCode in model.BillingCodes) 
     { 
      //do stuff with each of these 
     } 
    } 
+0

うーん:

@using (Html.BeginForm("SubmitTimesheet", "Timesheet", FormMethod.Post)) { @Html.EditorFor(m=>m.BillingCodes) } 

がEditorTemplates/BillingCodeObj.cshtmlで新しいCSHTMLを作成します。私はあなたが正しいと思うが、私が戻ってくるモデルオブジェクトのbillingCodesのリストはまだヌル..任意のアイデアですか? 質問を編集してコードを追加します。 –

0

編集:あなたがビューを切るモデルを送信する前に、あなたがnew List<BillingCode>()にごBillingCodeListのリスト要素を初期化していることを思えません。あなたですか?

+0

私はしています:BillingCodeList billingCodeList = JsonConvert.DeserializeObject (結果); –

関連する問題