2013-07-25 72 views
0

我正在使用HTML在線註冊表單。根據值的集合,在knockout.js中使用foreach數據綁定動態添加一些字段。我有完整的jquery部分,以便它將添加所有的文本字段和集合。Request.Form不返回值

形式的動態部分看起來是這樣的:

<div class="form_section" ID="FamilyMembers" style="display:none;" > 
     <div class="wFamilyMember" data-bind="foreach: $root.familyMembers" > 
      <h2>Family Member <span data-bind="text: ($index() + 1)"></span> Information</h2> 
      <div class='form_section'> 
       <div class="eccform_column"> 
        <div class="eccform_label">First Name*</div> 
        <div class="eccform_field"><input class="fmFirstName" title="First Name" type="text" data-bind="value:FirstName" /></div> 
       </div> 
       <div class="eccform_column"> 
        <div class="eccform_label">Last Name *</div> 
        <div class="eccform_field"><input class="fmLastName" title="Last Name" data-bind="value:LastName"/></div> 
       </div> 
      </div> 
      <div class="form_section"> 
       <div class="eccform_column"> 
        <div class="eccform_label">Family Role*:</div> 
        <div class="eccform_dropdowns"><select class='fmFamilyRole' data-bind="options: $root.familyRoles, optionsText:'roleName', optionsValue:'value', value:FamilyRole"></select> </div> 
       </div> 
       <div class="eccform_column"> 
        <div class="eccform_label">Age Group*:</div> 
        <div class="eccform_dropdowns"><select class='fmAgeGroup' data-bind="options:$root.ageGroups, optionsText:'groupName', optionsValue:'value', value:AgeGroup"></select></div> 
       </div> 
       <div class="eccform_checkbox_items"> 
        <div class="eccform_checkbox"><input type="checkbox" class="fmLunchTicket" data-bind="checked: LunchTicket">Lunch Ticket ($5 for children, $10 for adults)</div> 
        <div class="eccform_checkbox"><input type="checkbox" class="fmSeminarTicket" data-bind="checked: SeminarTicket" >Seminar Ticket ($40)</div> 
       </div> 
      </div> 
      <div class="form_section"> 
       <div class="eccform_column_button"> 
        <button data-bind="click: $root.removeFamilyMember">Remove</button> 
       </div> 
      </div> 
     </div> 
     <button data-bind="click: $root.addFamilyMember">Add Family Member</button> 
    </div> 

    <div class="form_section" ID="Exhibitors" style="display:none;"> 
     <div class='wExhibitor' data-bind="foreach: $root.exhibitors"> 
      <h2>Exhibitor <span data-bind="text: $index() + 1"></span> Information</h2> 
      <div class="eccform_section"> 
       <div class="eccform_column"> 
        <div class="eccform_label">First Name*</div> 
        <div class="eccform_field"><input class="exFirstName" title="First Name" data-bind="value:FirstName" /></div> 
       </div> 
       <div class="eccform_column"> 
        <div class="eccform_label">Last Name*:</div> 
        <div class="eccform_field"><input class="exLastName" title="Last Name" data-bind="value:LastName" /></div> 
       </div> 
      </div> 
      <div class="eccform_section"> 
       <div class="eccform_column_exhibitor"> 
        <div class="eccform_checkbox"><input type="checkbox" class="exLunchTicket" data-bind="checked: LunchTicket">Lunch Ticket ($10)</div> 
       </div> 
       <div class="eccform_column_exhibitor"> 
        <div class="eccform_checkbox"><input type="checkbox" class="exSeminarTicket" data-bind="checked: SeminarTicket">Seminar Ticket ($40)</div> 
       </div> 
       <div class="eccform_column_removebutton"> 
        <button data-bind="click: $root.removeExhibitor">Remove Exhibitor</button> 
       </div> 
      </div> 
     </div> 
     <button data-bind="click:$root.addExhibitor">Add Exhibitor</button> 
    </div> 

的問題是,當我試圖找回在C#中的值,我似乎不能夠讀取這些動態添加值。我用這個代碼來讀取值:

   List<string> oFirstNames = ConvertFromEnumerable(Request.Form.AllKeys.Where(x => x.StartsWith("exFirstName"))); 
       List<string> oLastNames = ConvertFromEnumerable(Request.Form.AllKeys.Where(x => x.StartsWith("exLastName"))); 
       List<string> oLunchTickets = ConvertFromEnumerable(Request.Form.AllKeys.Where(x => x.StartsWith("exLunchTicket"))); 
       List<string> oSeminarTickets = ConvertFromEnumerable(Request.Form.AllKeys.Where(x => x.StartsWith("exSeminarTicket"))); 

這工作時,我使用JavaScript動態添加新的元素,但它現在不工作。它總是返回一個空或空字符串集合。我覺得我從完全錯誤的角度來看待這個問題,但我不知道什麼是正確的角度。我應該找到一種方法從視圖模型中獲取數據嗎?任何見解?

+0

你嘗試過類似的Request.Form [ 「exFirstName」] – legrandviking

+0

我沒有。那會返回一個值還是數組?這種形式可以爲其添加可變數量的人員,因此嘗試只獲取一個元素可能會產生問題。 – ijb109

+0

無論如何,它只會返回該字段的值。也許使用Select而不是在哪裏可以提供幫助?你有沒有試過看看Request.Form.AllKeys裏面的內容,看看你是否有價值。此外,在我看來,有一個具有多個字段的同一名稱的表單必然會產生問題。 – legrandviking

回答

0

好吧,解決方案非常簡單,只需將相應的名稱附加到HTML DOM元素上即可。最終的HTML最終看起來像這樣:

<div class="form_section" ID="Exhibitors" style="display:none;"> 
     <div class='wExhibitor' data-bind="foreach: $root.exhibitors"> 
      <h2>Exhibitor <span data-bind="text: $index() + 1"></span> Information</h2> 
      <div class="eccform_section"> 
       <div class="eccform_column"> 
        <div class="eccform_label">First Name*</div> 
        <div class="eccform_field"><input id="exFirstName" runat="server" class="exFirstName" title="First Name" data-bind="value:FirstName,attr:{name: 'exFirstName_'+$index()}" /></div> 
       </div> 
       <div class="eccform_column"> 
        <div class="eccform_label">Last Name*:</div> 
        <div class="eccform_field"><input id="exLastName" runat="server" class="exLastName" title="Last Name" data-bind="value:LastName, attr:{name: 'exLastName_'+$index()}" /></div> 
       </div> 
      </div> 
      <div class="eccform_section"> 
       <div class="eccform_column_exhibitor"> 
        <div class="eccform_checkbox"><input id="exLunchTicket" name="exLunchTicket" runat="server" type="checkbox" class="exLunchTicket" data-bind="checked: LunchTicket, attr: { name: 'exLunchTicket_' + $index() }">Lunch Ticket ($10)</div> 
       </div> 
       <div class="eccform_column_exhibitor"> 
        <div class="eccform_checkbox"><input id="exSeminarTicket" name="exLunchTicket" runat="server" type="checkbox" class="exSeminarTicket" data-bind="checked: SeminarTicket, attr: { name: 'exSeminarTicket_' + $index() }">Seminar Ticket ($40)</div> 
       </div> 
       <div class="eccform_column_removebutton"> 
        <button data-bind="click: $root.removeExhibitor">Remove Exhibitor</button> 
       </div> 
      </div> 
     </div> 
     <button data-bind="click:$root.addExhibitor">Add Exhibitor</button> 
    </div> 

聲明本節: attr: { name: 'exSeminarTicket_' + $index() }

然後,在C#我改正的代碼看起來像這樣:

int SomeKeys = Request.Form.AllKeys.Where(x=>x.StartsWith("exFirstName")).Count(); 

for (int i = 0; i < SomeKeys; i++) 
{ 
    string FirstName = Request.Form["exFirstName_" + i]; 
    string LastName = Request.Form["exLastName_" + i]; 
    bool LunchTicket = Convert.ToBoolean(Request.Form["exLunchTicket_" + i]); 
    bool SeminarTicket = Convert.ToBoolean(Request.Form["exSeminarTicket_" + i]); 

    if (FirstName != "" && LastName != "") 
     lFamilyMembers.Add(new FamilyMember(FirstName, LastName, SeminarTicket, LunchTicket, AgeGroup.Twenties, FamilyRole.Exhibitor)); 
}