2014-11-05 32 views
0

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

查看:

<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 
+0

您可以显示''或'@ Html.BeginForm ...视图的'一部分? – DavidG 2014-11-05 01:21:52

+0

为什么你这样做有JavaScript填充两个隐藏的领域,而不是简单的通过邮件提交表格? – Kirby 2014-11-05 01:35:47

+0

@DavidG它的

在表顶部 – anonymous1110 2014-11-05 02:07:41

回答

0

除非你有JS的原因,检查出已先后建成什么asp.net的MVC英寸您的控制器的行动方法采取一些类型的集合作为参数,你通过使用一个for循环(不fo达到)与索引值。

看看这个老但糖果: http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

+0

我用javascript保留了以前在列表中添加的联系人。每当我添加一个新的联系人,并且get方法触发时,它会删除以前添加的文本框中的联系人。 – anonymous1110 2014-11-05 02:10:59

+0

你能告诉我一个样本吗? – anonymous1110 2014-11-05 02:16:08

+0

如果我明白你想创建一条消息给你选择正确的列表中的所有联系人?所以你会想要提交到CreateMessage操作方法? – Kirby 2014-11-05 02:23:29