0

在JavaScript中隱藏字段值被存儲。存儲隱藏字段值並在mvc的下一頁顯示它們

但是,當點擊下一頁隱藏字段爲空時,它不具有來自上一頁的值。

爲什麼它不會在下一頁加載以前的值?

如何在隱藏字段中加載以前的值?

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" /> 

回答

0

在看着你的代碼中,我注意到,可能會導致你的問題的問題你表明:

var index = CustomerIDArray.indexOf(currentCheckboxValue); 

然後使用索引來決定如果ID是陣列,通過這樣做:

if (index == 0) { 

它應該是

if (index == -1) {

as -1 found found not found。有了它,因爲它是目前,這將打擊該行:

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

,並嘗試拼接負指數,從字符串,這將使意外的結果的末尾向後推移。

0

您的隱藏hfCustomerID字段總是呈現給瀏覽器爲空字段,因爲您尚未將其綁定到您的Model中的任何內容。

我假設這張表是在張貼到您的控制器的表格裏面?如果是這樣,那麼你可以在模型中添加一個新字段,然後使用@Html.HiddenFor來呈現一個隱藏的輸入字段,該字段綁定到模型中的該項。

另外,它看起來像這個領域是完全計算基於勾選的複選框?在這種情況下,更新您的視圖以設置隱藏字段的值(但這會重複JavaScript中的某些邏輯)。

+0

你能舉一些例子嗎? –

+0

@ShridharSalunkhe如果您更新問題以顯示整個.cshtml視圖和您的控制器代碼,我將能夠更輕鬆地向您展示我的意思。 –

+0

在這個表格中,當勾選框被勾選,然後勾選框的值被存儲在使用JavaScript的隱藏字段中。我使用mvcpaging nuget包進行分頁。當我檢查值時,它存儲在隱藏字段,當我去到下一頁隱藏字段是空的。我想獲取上一頁的檢查值並在隱藏字段中添加新的檢查值 –

相關問題