Json,会话,Ajax调用不正确

时间:2019-07-19 22:39:46

标签: c# ajax view asp.net-mvc-5 controller

具有一个参数的索引参数化方法(又称为另一种方法)是ajax方法。 并且ajax成功并将数据显示在视图页面上。 但是在恢复索引方法后,参数值变为null。我想保持显示ajax返回。这是我的HomeController:

public ActionResult Index(int? customerNumber)
    {
        if(customerNumber.HasValue)
            DisplayCustomerInfo (customerNumber);       
        ViewBag.customerInfo = Session["CustomerInfo"];
        return View(ViewBag);
    }


[HttpPost]
    public string DisplayCustomerInfo(int? customerNumber) 
    {
        Customer customer = new Customer(); 
        customer = (Customer)client.LoadCustomerByNumber (Convert.ToInt32(customerNumber));
        Session["CustomerInfo"] = customer; 
        return new JavaScriptSerializer().Serialize(customer);
    } 



$(document).ready(function () {
    $("#txtcustomerID").on("keydown", function search(e) {
        if (e.keyCode == 13) {
            var customerID = $(this).val();
            $.ajax({
                type: "POST",
                url: "/Home/DisplayCustomerInfo/" + customerID,
                contentType: "application/json",
                data: "{'customerNumber' :'" + customerID + "'}",
                dataType: "JSON",
                success: function (data) {
                    $("#txtcustomerID").html(data.customerNumber);
                    $("#txtcustomerName").html(data.FullName);
                    $("#txtProdOffice").html(data.ProducingOfficeNumber + '-' + data.StateCode);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert("error" + textStatus + "-" + errorThrown);
                }
            });
        }
    });
});


<table id="tblcustomerinfo">
   @if(ViewBag.customerInfo==null){
   <tr>
      <th>CustomerNumber
      <th>
      <td><input type="text" id="txtcustomerID"/></td>
   </tr>
   } else{
   <tr>
      <td>Customer Number<input type="text" id="txtcustomerID"value=@ViewBag.customerInfo.Number/></td>
      <td>Customer Name id="txtcustomerName"> @ViewBag.customerInfo.FullName</div></td>
      <td>
         Producing Office
         <div id="txtProdOffice">@ViewBag.customerInfo.ProducingOfficeNumber-@ViewBag.customerInfo.StateCode</div>
      </td>
   </tr>
   } 
</table>

0 个答案:

没有答案