处理某些表单元素以构建对象 [英] Processing certain form elements to build objects
问题描述
全部
我有一个表单的一部分,用户可以在其中动态添加元素.他们能够一次添加包含3个输入字段的行.请注意,我没有将这些动态元素绑定到模型-我将在HTTP POST处理控制器中进行处理.
I have a section of a form where the user can add elements dynamically. They are able to add rows containing 3 input fields at a time. Note that I am not binding these dynamic elements to the model - I will handle in the HTTP POST processing controller.
每个添加的行代表一个共同发起人对象,该对象需要在表单处理控制器对HTTP POST的操作,然后将其插入数据库.
Each added row represents a Co-sponsor object that will need to be created in the form processing controller action for the HTTP POST and then inserted into the database.
我想知道命名这些元素的最佳方法是什么,以便可以在处理控制器中轻松创建对象?
I was wondering what is the best way to name these elements so I can easily create the objects in the processing controller?
当前,元素的命名如下:
Currently, the elements are being named like this:
cosponsorcontact_1
cosponsoremail_1
cosponsorphone_1
cosponsorcontact_2
cosponsoremail_2
cosponsorphone_2
cosponsorcontact_4
cosponsoremail_4
cosponsorphone_4
cosponsorcontact_n
cosponsoremail_n
cosponsorphone_n
这将涉及大量的正则表达式和循环,以将它们放回原处,从而创建对象.我假设这些元素可能以随机顺序出现在Request.Form集合中,并且我们可能没有ID的顺序编号,如上面的示例所示.
This will involve a lot of regexing and looping to put these back together and thus create the objects. I am assuming that these elements may be in the Request.Form collection in a random order and that we may not have sequential numbering of the ids as shown in the example above.
我当然可以那样做,但是我确信必须有一种更简单的方法,也许是在表单元素的命名约定中?
I can certainly do it that way but I am sure that there has to be an easier way, perhaps in the naming convention of the form elements?
以下是生成的表单内容的摘要:
<div id="cosponsors_6" style="display: block;">
<label>Sponsor club name</label></span>
<input type="text" title="Co-sponsor contact" placeholder="Name" name="cosponsorcontact_6" id="cosponsorcontact_6">
<input type="text" title="Co-sponsor email" placeholder="Email" name="cosponsoremail_6" id="cosponsoremail_6">
<input type="text" title="Co-sponsor phone" placeholder="Phone" name="cosponsorphone_6" id="cosponsorphone_6"><a class="icon delete"></a>
</div>
<div id="cosponsors_7" style="display: block;">
<label>Sponsor club name</label></span>
<input type="text" title="Co-sponsor contact" placeholder="Name" name="cosponsorcontact_7" id="cosponsorcontact_7">
<input type="text" title="Co-sponsor email" placeholder="Email" name="cosponsoremail_7" id="cosponsoremail_7">
<input type="text" title="Co-sponsor phone" placeholder="Phone" name="cosponsorphone_7" id="cosponsorphone_7"><a class="icon delete"></a><
</div>
<div id="cosponsors_8" style="display: block;">
<label>Sponsor club name</label></span>
<input type="text" title="Co-sponsor contact" placeholder="Name" name="cosponsorcontact_8" id="cosponsorcontact_8">
<input type="text" title="Co-sponsor email" placeholder="Email" name="cosponsoremail_8" id="cosponsoremail_8">
<input type="text" title="Co-sponsor phone" placeholder="Phone" name="cosponsorphone_8" id="cosponsorphone_8"><a class="icon delete"></a>
</div>
<div id="cosponsors_9" style="display: block;">
<label>Sponsor club name</label></span>
<input type="text" title="Co-sponsor contact" placeholder="Name" name="cosponsorcontact_9" id="cosponsorcontact_9">
<input type="text" title="Co-sponsor email" placeholder="Email" name="cosponsoremail_9" id="cosponsoremail_9">
<input type="text" title="Co-sponsor phone" placeholder="Phone" name="cosponsorphone_9" id="cosponsorphone_9"><a class="icon delete"></a>
</div>
推荐答案
Phil Haack's article on model binding to a list covers posting back non-sequential list items.
<input type="hidden" name="cosponsorContacts.Index" value="3" />
<input type="text" name="cosponsorContacts[3].contact" value="Whatever" />
<input type="text" name="cosponsorContacts[3].email" value="Here" />
<input type="text" name="cosponsorContacts[3].phone" value="Digits" />
<input type="hidden" name="cosponsorContacts.Index" value="7" />
<input type="text" name="cosponsorContacts[7].contact" value="One" />
<input type="text" name="cosponsorContacts[7].email" value="Two" />
<input type="text" name="cosponsorContacts[7].phone" value="Three/>
这篇关于处理某些表单元素以构建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!