【发布时间】:2015-01-01 02:40:03
【问题描述】:
我有一个带有复选框的表格,其中包含很多联系人列表,该复选框可供他们选择要在下面的文本框中显示的特定联系人。我的问题是,当我尝试单击添加按钮时,它会抛出一个错误,即我的 url 太长,主要是因为它似乎试图通过查询字符串解析每个输入。还有其他方法可以使这项工作吗?
查看:
<table id="tblcontacts>
<tr>
<th colspan="1">
<input type="checkbox" id="checkall" /><span>Select All</span>
</th>
<th colspan="2"></th>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.contacts.First().Selected)
</th>
<th>
@Html.DisplayNameFor(model => model.contacts.First().ContactName)
</th>
<th>
@Html.DisplayNameFor(model => model.contacts.First().ContactNo)
</th>
</tr>
@foreach (var item in Model.contacts)
{
<tr>
<td style="text-align: center">
@Html.CheckBoxFor(modelItem => item.Selected)
</td>
<td id="contactname">
@Html.DisplayFor(modelItem => item.ContactName)
</td>
<td id="contactnos">
@Html.DisplayFor(modelItem => item.ContactNo)
</td>
</tr>
}
</table>
@* Populate Contacts via Jquery(Commonscript.js) during add click *@
<input id="hfContacts" name="hfContacts" type="hidden" />
<input id="hfContactnos" name="hfContactnos" type="hidden" />
<div class="buttons">
<input id="btnadd" type="submit" value="Add" />
</div>
Javascript:
$('#btnadd').click(function () {
//get txtMessageTo value
contactname = $('#txtMessageTo').val() || "";
if (contactname != "")
contactname += ',';
//check <tr> for checked status where <td id="contact">
$('#tblcontacts tr').filter(':has(:checkbox:checked)').children('#contactname').each(function () {
thisContact = $(this);
// Checks if contact was previously added already.
if (!(contactname.indexOf($.trim(thisContact.text())) > -1))
contactname += $.trim(thisContact.text()) + ',';
});
$('#hfContacts').val(contactname);
});
控制器:
public ActionResult CreateMessage(string hfContacts, string hfContactnos)
//adds contacts to model to be displayed on a separate textbox form
【问题讨论】:
-
您能显示视图的
<form...>或@Html.BeginForm...部分吗? -
你为什么这样做让 JavaScript 填充两个隐藏字段而不是简单地通过 post 提交表单?
-
@DavidG 其
标签: asp.net-mvc-4 checkbox input query-string