2013-04-22 148 views
0

這應該是一個簡單的操作,此時我的程序感覺不一樣。當用戶在我的視圖中輸入任意數量的文本時,他們應該能夠點擊放大鏡,將它們發送到控制器的文本發送到控制器,然後調用Web服務並執行公司名稱搜索。jQuery/Ajax將未定義參數傳遞給ASP MVC控制器

下面是我創建的一個函數,它只是向ASP MVC 3控制器發送兩個參數。當我在控制器中的Chrome調試器和Visual Studio中查看變量searchItem時,我可以看到它是nullundefined,但第二項始終沒有問題。

function GetCompDetails() { 
    var searchItem = $('#DRMCompanyId').val; 
    var request = $.ajax({ 
     type: 'POST', 
     url: '@Url.Action("compSearch", "AgentTransmission")', 
     data: 
      { 
       searchFilter: searchItem, 
       url: location.protocol + '//' + location.host 
      }, 
     dataType: 'html', 
     success: function (data) { 
      alert(data); 
     }, 
     error: function (data) { 
      alert("Unable to process your resquest at this time."); 
     } 
    }); 
} 

這裏是我使用應經過searchItem參數<div>。正如你所看到的,我嘗試了兩種不同的方法來創建文本框/輸入區域。然而,兩者都通過undefined來傳遞參數。

任何幫助將不勝感激。

<div class="M-editor-field"> 
     <img src="@Url.Content("~/Content/Images/magnify.gif")" onclick="GetCompDetails()" /> 
     @Html.TextBoxFor(model => model.BankName, new { id = "DRMCompanyId" }) 
     @*@Html.EditorFor(model => model.DRMCompanyId)*@ 
     @Html.ValidationMessageFor(model => model.DRMCompanyId) 
    </div> 

這裏是我的控制器的方法簽名。 searchFilter目前undefined每次但url參數工作正常。

[HttpPost] 
    public string compSearch(string searchFilter, string url) 
    { 

回答

1

您有一個錯誤在你的JavaScript,在你的第一行,你忘了pernthesis

function GetCompDetails() { 
    var searchItem = $('#DRMCompanyId').val(); // You need to add parenthesis if you call a function 
    var request = $.ajax({ 
     type: 'POST', 
     url: '@Url.Action("compSearch", "AgentTransmission")', 
     data: 
      { 
       searchFilter: searchItem, 
       url: location.protocol + '//' + location.host 
      }, 
     dataType: 'html', 
     success: function (data) { 
      alert(data); 
     }, 
     error: function (data) { 
      alert("Unable to process your resquest at this time."); 
     } 
    }); 
} 
+0

哈,好,謝謝。想象它會是這樣的(過了漫長的一天......) – NealR 2013-04-22 22:28:15

+0

好吧,完美!樂於幫助。如果答案對你有幫助,不要忘記接受答案和/或祝好。 – Kenneth 2013-04-22 22:29:06

相關問題