2016-11-23 173 views
0

I'm使搜索框獲得視的價值我的數據庫的一個寄存器sended,所以在我看來:獲取參數控制器

<p> Manifest: <input type="text" name="manifest" id="manifest" /> <input type="submit" value="search" name="btnGetForEdith" id="btnGetForEdith" /></p> 

JS:

$(document).ready(function() { 
    GetModulLogWasteForEdit(); 
    $("#btnGetForEdith").click(onGetModulLogWasteSuccess); 
}); 

function GetModulLogWasteForEdit() { 
    currentId = 0; 
    try { 
     $(function() { 
      $.ajax({ 
       cache: false, 
       type: "get", 
       dataType: "json", 
       url: "/LogWaste/GetForEdit", 
       contentType: "application/json; charset=utf-8", 
       success: onGetModulLogWasteSuccess, 
       error: function (response) { 
        ErrorMessage("Error", GetTextError(response)); 
       } 
      }); 
     }); 
    } catch (e) { 
     ErrorMessage("Error", e.message); 
    } 
} 

控制器:

public ActionResult GetForEdit(string manifest) 
     { 
      string result = string.Empty; 
      var userId = User.Identity.GetUserId(); 
      var currentUser = UserClass.GetUserBranchOfficeId(userId); 
      try 
      { 
       result = new JavaScriptSerializer().Serialize(LogWasteModule.GetForEdit(currentUser)); 
      } 
      catch (Exception) 
      { 
       throw; 
      } 

      return Content(result, "application/json"); 
     } 

問題是我不明顯的價值進入我的控制器它來了null所以我不能與它一起。任何人都可以解釋爲什麼會發生?問候

+0

你缺少對Ajax調用數據參數...請參見本:http://stackoverflow.com/questions/15576548/how-to-pass-parameters-in-get-requests-with-jquery –

回答

0

嘗試執行匿名函數,你沒把Ajax請求

function GetModulLogWasteForEdit() { 
currentId = 0; 
try { 
    $(function() { 
     $.ajax({ 
      cache: false, 
      type: "get", 
      dataType: "json", 
      url: "/LogWaste/GetForEdit", 
      contentType: "application/json; charset=utf-8", 
      success: onGetModulLogWasteSuccess, 
      error: function (response) { 
       ErrorMessage("Error", GetTextError(response)); 
      } 
     }); 
    })(); // Add() to execute the function 
} catch (e) { 
    ErrorMessage("Error", e.message); 
} 

}

+0

這不是問題,問題是我沒有收到輸入搜索的「清單」值,我在按「搜索」按鈕之前編寫(視圖)到我的控制器 –

0
function GetModulLogWasteForEdit() { 
    currentId = 0; 


try { 
    var manifest=$('#manifest').val(); 

     $.ajax({ 
      cache: false, 
      type: "get", 
      dataType: "json", 
      url: "/LogWaste/GetForEdit", 
      data:{manifest:manifest} 
      contentType: "application/json; charset=utf-8", 
      success: onGetModulLogWasteSuccess, 
      error: function (response) { 
       ErrorMessage("Error", GetTextError(response)); 
      } 
     }); 
    } catch (e) { 
    ErrorMessage("Error", e.message); 
    } 
} 
+0

好主意,但它仍然爲空:/ –

+0

噸ry: data:'{「manifest」:「'+ manifest +'」}' –

0

爲了得到你有送價值它,我不看你在做什麼。補充一點:

data: {'manifest': manifest } 

因此,它應該是這樣的:

$(function() { 
     $.ajax({ 
      cache: false, 
      type: "get", 
      dataType: "json", 
      url: "/LogWaste/GetForEdit", 
      data: {'manifest': manifest } 
      contentType: "application/json; charset=utf-8", 
      success: onGetModulLogWasteSuccess, 
      error: function (response) { 
       ErrorMessage("Error", GetTextError(response)); 
      } 
     }); 
    }); 

我希望這有助於!

0

請在Javascript

$(document).ready(function() {   
     $("#btnGetForEdith").click(function() { 
      GetModulLogWasteForEdit(); 
     }); 
    }); 

    function GetModulLogWasteForEdit() { 
     currentId = 0; 
     try { 
      $(function() { 
       $.ajax({ 
        cache: false, 
        type: "GET", 
        dataType: "json", 
        url: "/LogWaste/GetForEdit?manifest=" + $("#manifest").val(),//Manifest will be passed in querystring like this 
        contentType: "application/json; charset=utf-8", 
        success: function (data) { 
         console.log(data); 
        }, 
        error: function (response) { 
         console.log("Error", GetTextError(response)); 
        } 
       }); 
      }); 
     } catch (e) { 
      ErrorMessage("Error", e.message); 
     } 
    } 

使用下面的代碼在控制器

public ActionResult GetForEdit(string manifest) 
    { 
     string result = string.Empty; 
     var userId = User.Identity.GetUserId(); 
     var currentUser = UserClass.GetUserBranchOfficeId(userId); 
     try 
     { 
      result = new JavaScriptSerializer().Serialize(LogWasteModule.GetForEdit(currentUser)); 
     } 
     catch (Exception) 
     { 
      throw; 
     } 
     //Had changed this line as your are using `dataType: "json"`, as return data type 
     return Json(result, JsonRequestBehavior.AllowGet); 
    } 
相關問題