2014-06-19 57 views
1

我是新來的MVC和使用MVC4。我有一系列級聯下拉菜單,我正在使用AJAX調用。客戶>家庭>組裝 - 該組件確定需要用戶填寫的具有多少巴倫(具有屬性的對象),巴倫斯對象的數量基於該組件的要求。如何使用MVC4動態Html輸入

我爲每個項目創建了一個單獨的對象,併爲此視圖創建了一個視圖模型。然後,我將listOfBaluns傳遞給一個局部視圖來循環並創建HTML,但是需要很多HTML。

當我提交表單時,我總是隻看到第一個Balun對象的屬性。根據我的理解,我正確地使用局部視圖來遍歷Balun對象列表,但這些對於FormCollection或Request.Form都不會出現在Post上。

在我包含的代碼中,我只是測試以查看是否可以獲得2個baluns工作,這應該解釋爲什麼我明確將2個balunrecords添加到視圖模型列表中。

任何幫助,將不勝感激。

這裏是查看 -

@model Nortech_Intranet.ViewModels.BalunTuningEntryViewModel 
@using Nortech_Intranet.Models.BalunModels 

@using (Html.BeginForm()) { 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true) 

    <div class="whiteBodyContainer"> 
     @*Text Entry fields*@ 
     <table style="margin: auto;"> 
      <tr> 
       <th> 
        @Html.LabelFor(model => model.balunTuningRecord.Start_Timestamp) 
       </th> 
       <td> 
        @Html.TextBoxFor(model => model.balunTuningRecord.Start_Timestamp, new { @Value = @System.DateTime.Now }) 
       </td> 
       <th> 
        @Html.LabelFor(model => model.balunTuningRecord.Work_Order) 
       </th> 
       <td> 
        @Html.TextBoxFor(model => model.balunTuningRecord.Work_Order) 
       </td> 
       <th> 
        @Html.LabelFor(model => model.balunTuningRecord.Serial_Number) 
       </th> 
       <td> 
        @Html.TextBoxFor(model => model.balunTuningRecord.Serial_Number) 
       </td> 
      </tr> 
     </table> 
     <hr />  

     <table style="margin: auto;"> 
      <tr> 
       @Html.Partial("_RenderBalun", Model.balunList) 
      </tr> 
     </table> 
    </div> 

    <p> 
     <input type="submit" value="Create Tuning Record" /> 
    </p> 
} 

視圖模型 -

using Nortech_Intranet.Models.BalunModels; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace Nortech_Intranet.ViewModels 
{ 
    public class BalunTuningEntryViewModel 
    { 
     public List<Customer> customerList { get; set; } 
     public List<Family> familyList { get; set; } 
     public List<Assembly> assemblyList { get; set; } 
     public List<NWAnalyzerModel> nwAnalyzerModelList { get; set; } 
     public List<NWAnalyzerCAL_ID> nwAnalyzerCAL_IDList { get; set; } 
     public List<TestUser> testUserList { get; set; } 
     public List<BalunRecord> balunList { get; set; } 

     public BalunTuningRecord balunTuningRecord { get; set; } 

     public BalunTuningEntryViewModel() : base() 
     { 
      balunList = new List<BalunRecord>(); 

      BalunRecord newRecord = new BalunRecord(); 
      balunList.Add(newRecord); 
      BalunRecord newRecord2 = new BalunRecord(); 
      balunList.Add(newRecord2); 
     } 

     public BalunTuningEntryViewModel(int i) 
     { 
      balunList = new List<BalunRecord>(); 

      for (int counter = 0; counter < i; counter++) 
      { 
       BalunRecord newRecord = new BalunRecord(); 
       balunList.Add(newRecord); 
      } 
     } 
    } 
} 

管窺 -

@model IEnumerable<Nortech_Intranet.Models.BalunModels.BalunRecord> 
@using Nortech_Intranet.Models.BalunModels 

@{ 
    int counter = 1; 
    foreach(BalunRecord record in Model) 
    { 
    <td> 
     <table id="@("tblBalun"[email protected])" style="border: 2px solid gray; visibility: collapse;"> 
      <tr> 
       <th colspan="2" style="text-align: center;"><label>Balun #@counter</label></th> 
      </tr> 
      <tr> 
       <th> 
        @Html.LabelFor(model => record.Tuning_Cap_1) 
       </th> 
       <td> 
        @Html.TextBoxFor(model => record.Tuning_Cap_1, new { id = "ddlBalun" + @counter + "Cap" + "1" }) 
       </td> 
      </tr> 
      <tr> 
       <th> 
        @Html.LabelFor(model => record.Tuning_Cap_2) 
       </th> 
       <td> 
        @Html.TextBoxFor(model => record.Tuning_Cap_2, new { id = "ddlBalun" + @counter + "Cap" + "2" }) 
       </td> 
      </tr> 
     </table> 
    </td>  

     counter = counter + 1; 
    } 
} 
+0

爲什麼不簡化代碼並只顯示理解問題所需的最低要求?我認爲這個問題並不難解決,但沒有人會審查這麼多的代碼。至少我。購買方式:太多的測試,太多的代碼,但你的問題根本不明確。噸的代碼和...控制器的行動在哪裏? – JotaBe

+0

我試圖儘可能詳細,但很好的建議,我縮短了代碼,所以現在它得到了所有的脂肪。感謝您的建議。 – KLeintz

回答

0

我想通了,解決辦法是通過循環,並使用索引爲模型。這裏是解決問題的視圖中的新部分 -

@{ 
    var counter = 1; 
    for (int i = 0; i < Model.balunList.Count; i++) 
    { 
    <td> 
    <table id="@("tblBalun" + @counter)" style="border: 2px solid gray; visibility: collapse;"> 
     <tr> 
      <th colspan="2" style="text-align: center;"><label>Balun #@counter</label></th> 
     </tr> 
     <tr> 
      <th> 
       @Html.LabelFor(model => model.balunList[i].Tuning_Cap_1) 
      </th> 
      <td> 
       @Html.TextBoxFor(model => model.balunList[i].Tuning_Cap_1, new { id = "ddlBalun" + @counter + "Cap" + "1" }) 
      </td> 
     </tr> 
     <tr> 
      <th> 
       @Html.LabelFor(model => model.balunList[i].Tuning_Cap_2) 
      </th> 
      <td> 
       @Html.TextBoxFor(model => model.balunList[i].Tuning_Cap_2, new { id = "ddlBalun" + @counter + "Cap" + "2" }) 
      </td> 
     </tr> 
    </table> 
    </td> 
     counter++; 
    } 
}