【问题标题】:MVC avoiding querystring, url too longMVC 避免查询字符串、url 太长
【发布时间】: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

【问题讨论】:

  • 您能显示视图的&lt;form...&gt;@Html.BeginForm... 部分吗?
  • 你为什么这样做让 JavaScript 填充两个隐藏字段而不是简单地通过 post 提交表单?
  • @DavidG 其
    在表格顶部

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


【解决方案1】:

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

看看这个老歌但很好: http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

【讨论】:

  • 我使用 javascript 在列表中保留以前添加的联系人。因为每次我添加新联系人时,get 方法都会触发,它会删除文本框上之前添加的联系人。
  • 如果我了解您想向列表中您选择正确的所有联系人创建消息?那么您将要提交到 CreateMessage 操作方法吗?
  • 是的。我正在创建一个看起来像 Outlook 的表单。我的 tblcontacts 在手风琴窗格内,每次触发添加按钮时,都会触发 createmessage 的 get 方法。
  • 不在电脑前,所以我会尽力而为。将 CreateMessage 参数更改为 Contacts[] 联系人。在您的剃须刀中将 foreach 更改为以确保您引用诸如此contact[i].selected 等项目。当您提交时,您将拉出数组中selected = true 的所有行。然后,您可以在 action 方法中构建字符串并返回以在文本字段中使用该值。不用JS拦截提交。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-12
  • 1970-01-01
  • 2012-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多