2017-01-18 225 views
1

我在表單中有三個下拉列表,並且我希望不會丟失用戶選擇的數據,即使我正在刷新或在其他頁面中導航並返回。如何避免頁面刷新後丟失數據

這是我的看法,我有3個下拉菜單:

@using (Ajax.BeginForm("SearchForCars", "Home", null, new AjaxOptions { UpdateTargetId = "DivCategoriesTree", OnSuccess = "success", HttpMethod = "Post" }, new { make = "makes" })) 
{ 
      <div> 
        <div class="col-sm-4" style="height: 10em;display: flex;align-items: center ; padding-top:25px;"> 
         <i class="fa fa-car" style="font-size:60px;color:red; padding-left:20px;"></i> 
         <strong style="padding-left:20px;"></strong> 
        </div> 

        <div class="col-sm-4"> 
         <div style="padding-top:15px;"> 
          <form class="form-control-static"> 
           <div class="form-group"> 
            @*<div class="panel panel-default" id="Productresult">*@ 
            @*<div class="panel-body">*@ 
            <div class="row"> 
             <div class="col-sm-10"> 
              @if (ViewData.ContainsKey("makes")) 
              { 
               @Html.DropDownList("makes", ViewData["makes"] as List<SelectListItem>, "--Select make--", new { @class = "dropdown-toggle form-control" }) 
              } 
             </div> 
            </div> 
            <div class="row"> 
             <div class="col-sm-10"> 
              <p></p> 
              @Html.DropDownList("models", new SelectList(string.Empty, "Value", "Text"), "--Select model--", new { @class = "dropdown-toggle form-control" }) 
             </div> 
            </div> 
            <div class="row"> 
             <p></p> 
             <div class="col-sm-10"> 
              @Html.DropDownList("engines", new SelectList(string.Empty, "Value", "Text"), "--Select engine--", new { @class = "dropdown-toggle form-control" }) 
             </div> 
            </div> 
            @*</div>*@ 
            @*</div>*@ 
           </div> 

          </form> 

         </div> 
        </div> 

        <div class="col-sm-4" style="height: 10em;display: flex;align-items: center ; padding-top:25px;"> 
         <input type="submit" id="btnSearch" class="btn btn-default active" value="Cauta" disabled="disabled" style="width:150px;" /> 
        </div> 
       </div> 
      } 

我嘗試使用此代碼解決,但它並沒有解決這個問題:

window.onload = function() {   
    if (sessionStorage.getItem('makes') == "makes") { 
     return; 
} 

var makes = sessionStorage.getItem('makes'); 
    if (name !== null) $('#makes').val(makes); 
} 

window.onbeforeunload = function() { 
    sessionStorage.setItem("makes", $('#makes').val()); 
} 

你能請告知要檢查什麼,或者我該如何解決這個問題? 謝謝先進!

回答

1

您可以使用TempData []。除非將其設置爲空,否則您甚至可以在其他頁面上傳遞該值。