【问题标题】:store hidden field value and show them on next page in mvc存储隐藏字段值并在 mvc 的下一页上显示它们
【发布时间】:2017-05-03 11:24:43
【问题描述】:

在 JavaScript 中存储隐藏字段值。

但是当点击下一页隐藏字段为空时,它没有上一页的值。

为什么它不在下一页加载以前的值?

如何在隐藏字段中加载之前的值??

function AddRemoveCustomer(id) {
  //$(".checkBoxClass").click(function (e) {
  alert('in main function');
  alert(id);

  var CustomerIDArray = [];
  var hidCID = document.getElementById("hfCustomerID");
  if (hidCID != null && hidCID != 'undefined') {
    var CustID = hidCID.value;
    CustomerIDArray = CustID.split("|");
    var currentCheckboxValue = id;
    var index = CustomerIDArray.indexOf(currentCheckboxValue);
    alert('index value:' + index);
    debugger;
    if (index == 0) {
      alert('if');
      CustomerIDArray.push(currentCheckboxValue);
      alert('pushed value:' + CustomerIDArray);
    } else {
      alert('else');
      var a = CustomerIDArray.splice(index, 1);
      alert("a" + a);

    }
    hidCID.value = CustomerIDArray.join("|");
    alert('Final' + hidCID.value);

  } else {
    alert('undefined');
  }
  //});
}
<table id="tblEmailScheduler" class="table-bordered col-offset-12">
  <thead>
    <tr class="label-primary">
      <th style="padding:5px 15px;">
        First Name
      </th>
      <th style="padding:5px 15px;">
        Last Name
      </th>
      <th style="padding:5px 15px;">
        Email ID
      </th>
      <th style="padding:5px 15px;">
        Customer Type
      </th>
      <th style="padding:5px 15px;">
        Customer Designation @Html.DropDownList("CustomerDesignation", new SelectList(ViewBag.SelectAllCustomerDesignationDDL, "Value", "Text"), new { id = "CustomerDesignationDDL" , name = "CustomerDesignationDDL" })
      </th>
      <th style="padding:5px 15px;">
        Select All
        <div class="checkbox control-group">
          <label><input type="checkbox" id="cbSelectAll" /></label>
        </div>
      </th>

    </tr>
  </thead>
  <tfoot>
    <tr>
      <th colspan="2">
        EmailTemplate : @Html.DropDownList("EmailSubject", new SelectList(ViewBag.SelectAllEmailTemplateDDL, "Value", "Text"), new { id = "SelectAllEmailTemplateDDL" })
      </th>
      <th colspan="2">
        Set Date And Time:
        <input type="text" class="from-date-picker" readonly="readonly" />
      </th>
      <th colspan="2">
        <input type="submit" value="Schedule" id="btnSubmit" class="btn btn-default" />
      </th>
      <td>

      </td>
    </tr>
  </tfoot>
  @foreach (var item in Model) {
  <tr style="text-align:center">
    <td id="tblFirstName">
      @item.FirstName
    </td>
    <td id="tblLastName">
      @item.LastName
    </td>
    <td id="tblEmailID">
      @item.EmailID
    </td>
    <td id="tblCustomerType">
      @item.CustomerType
    </td>
    <td id="tblCustomerDesignation">
      @item.CustomerDesignation
    </td>
    <td>
      <div class="checkbox control-group">
        <label><input type="checkbox" id="@item.CustomerID" value="@item.CustomerID" 
        onclick="AddRemoveCustomer(@item.CustomerID)" class="checkBoxClass"/>
        @*@Html.CheckBox("Select", new { id = "cbCustomer", item.CustomerID})*@</label>
      </div>
    </td>
  </tr>
  }
</table>
<input type="hidden" id="hfCustomerID" />

【问题讨论】:

    标签: javascript asp.net-mvc model-view-controller hidden-field


    【解决方案1】:

    在查看您的代码时,我注意到一个问题可能会导致您所指出的问题:

    var index = CustomerIDArray.indexOf(currentCheckboxValue);
    

    然后,您可以使用索引来确定 Id 是否在数组中,方法是:

    if (index == 0) {
    

    应该是

    if (index == -1) {

    -1 表示未找到。就目前的情况而言,它会达到这一点:

    var a = CustomerIDArray.splice(index, 1);
    

    并尝试拼接一个负索引,该索引从字符串的末尾向后移动,这会产生意想不到的结果。

    【讨论】:

      【解决方案2】:

      您隐藏的 hfCustomerID 字段始终作为空字段呈现给浏览器,因为您没有将其绑定到您的 Model 中的任何内容。

      我假设这个表格在一个表格中,该表格正在发布到您的控制器?如果是这样,那么您可以向您的模型添加一个新字段,然后使用@Html.HiddenFor 呈现一个隐藏的输入字段,该字段绑定到您模型中的该项目。

      或者,看起来这个字段完全是根据勾选的复选框计算的?在这种情况下,请更新您的视图以设置隐藏字段的值(尽管这会复制 JavaScript 中的一些逻辑)。

      【讨论】:

      • 你能举个例子吗?
      • @ShridharSalunkhe 如果您更新问题以显示整个 .cshtml 视图和您的控制器代码,我将能够更轻松地向您展示我的意思。
      • in this table,when checked box checked then checked box value is stored in hidden field using JavaScript.我使用 mvcpaging nuget 包进行分页。所以当我检查值时,当我转到下一页隐藏字段为空时,它存储在隐藏字段中。我想获取上一页的检查值并在隐藏字段中添加新的检查值
      • 我不熟悉 MvcPaging 包,但看起来每次用户更改页面时它都会重新加载整个视图。这将丢弃隐藏的输入值。所以你需要找到一些其他的存储方式(也许在 JavaScript 中使用 localStorage?),或者以不同的方式进行分页。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多