MVC避免查询,url太长

时间:2014-11-05 01:17:59

标签: asp.net-mvc-4 checkbox input query-string

我有一个带有复选框的表格,其中包含大量的联系人列表,复选框可供他们选择要在下面的文本框中显示的特定联系人。我的问题是,当我尝试单击添加按钮时,它会抛出一个错误,我的网址太长,主要是因为它似乎试图通过查询字符串解析每个输入..是否有另一种方法使这个工作?

查看:

 <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

1 个答案:

答案 0 :(得分:0)

除非您有JS的理由,否则请查看asp.net mvc已经内置的内容。您的控制器操作方法将某种类型的集合作为参数进行处理,并通过使用带有索引的for(不是foreach)进行html循环值。

看看这个老人但是好东西: http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx