我正在使用HTML进行在线注册表单。一些字段是使用knockout.js中的foreach数据绑定根据值集合动态添加的。我已经完成了jquery部分,因此它将添加所有文本字段和集合。
表单的动态部分如下所示:
<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动态添加新元素之前,这有效,但它现在不起作用。它总是返回null或空字符串集。我觉得我从完全错误的角度接近这个,但我不知道正确的角度是什么。我应该找到一种从viewmodel获取数据的方法吗?任何见解?
答案 0 :(得分: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));
}