2014-01-09 19 views
0

的mvc表數據不發佈到我在我的觀點如下CSHTML的Asp.net的Web API控制器

<div> 
    @(Html.Kendo().DropDownList() 
      .Name("Designation") 
      .DataValueField("Designation") 
      .DataTextField("Designation") 
      .SelectedIndex(0) 
      .BindTo((System.Collections.IEnumerable)ViewData["Designation"])) 
    @(Html.Kendo().DropDownList() 
      .Name("DeptId") 
      .DataValueField("DeptId") 
      .DataTextField("DeptName") 
      .SelectedIndex(0) 
      .BindTo((System.Collections.IEnumerable)ViewData["Department"])) 
    <input class="k-button" id="btnFilter" type="submit" value="Filter" /> 
</div> 

我想這兩個下拉列表的值張貼到我的網站ApiController。我創建了以下jquery ajax方法來調用api。但它不起作用。

JQuery的:

$(document).ready(function() { 
     $("#btnFilter").click(function() { 
      debugger; 
      var designation = $("#Designation").val(); 
      var deptname = $("#DeptId").val(); 
      $.ajax({ 
       url: "http://localhost:8648/api/Employee" + deptname + designation, 
       type: "Post", 
       // data: JSON.stringify([designation, deptname]), //{ Name: name, 
       // Address: address, DOB: dob }, 
       contentType: 'application/json; charset=utf-8', 
       success: function (data) { alert("posted") }, 
       error: function() { alert('error'); } 
      }); 
     }); 
    }); 

這裏是我的API控制器POST方法:

public HttpResponseMessage PostEmployee(EmployeeViewModel newEmployee, String deptname, String designation) 
     { 
      //code 
     } 

哪能dropdownlost的值發送給我的ApiController。

+0

'API Controller'不能接受這樣的參數。將'deptname'和'designation'添加爲您的'EmployeeViewModel'的屬性或完全創建新模型。 –

回答

0

您使用GET方法,而你的API方法是POST

您的請求將會像這樣的事情

$(document).ready(function() { 
     $("#btnFilter").click(function() { 
      debugger; 
      var designation = $("#Designation").val(); 
      var deptname = $("#DeptId").val(); 
      $.ajax({ 
       url: "http://localhost:8648/api/Employee/PostEmployee", 
       type: "Post", 
       data: {"newEmployee":/*your serialized data for the model*/, 
         "deptname":deptname ,"designation":designation } 
       contentType: 'application/json; charset=utf-8', 
       success: function (data) { alert("posted") }, 
       error: function() { alert('error'); } 
      }); 
     }); 
    }); 
+0

它不起作用,我試了兩個帖子,並得到 –

+0

你沒有在你的請求中指定方法名稱 – Cris

0

根據你的問題,你要發佈僅下拉數據則 試試這個

$(document).ready(function() { 
     $("#btnFilter").click(function() { 
      debugger; 
      var designation = $("#Designation").val(); 
      var deptname = $("#DeptId").val(); 
      $.ajax({ 
       url: "http://localhost:8648/api/Employee?deptname="+deptname+"&designation="+designation , 
       type: "Post",      
       contentType: 'application/json; charset=utf-8', 
       success: function (data) { alert("posted") }, 
       error: function() { alert('error'); } 
      }); 
     }); 
    }); 

Api控制器

public HttpResponseMessage PostEmployee(String deptname, String designation) 
     { 
      //code 
     } 
0

您正試圖發送數據作爲請求url的一部分。只有在您提出獲取請求時纔會這樣做。

如果您希望這樣做,因爲你需要在你的$.ajax函數來指定,這是一個GET請求,並正確地構造URL的GET請求:

$.ajax({ 
      //construct the url with the data as part of the query string 
      url: "http://localhost:8648/api/Employee?deptname="+deptname+"&designation="+designation , 
      //specify it's a get request 
      type: "GET",      
      contentType: 'application/json; charset=utf-8', 
      success: function (data) { alert("posted") }, 
      error: function() { alert('error'); } 
     }); 
}); 

隨着您的API控制器是這樣的:

[HttpGet] 
public HttpResponseMessage PostEmployee(String deptname, String designation) 
{ 

} 

但是,如果你想這樣做,因爲POST請求,你需要在請求的主體發送數據:

$.ajax({ 
      url: "http://localhost:8648/api/Employee, 
      //specify it's a POSTrequest 
      type: "POST",      
      contentType: 'application/json; charset=utf-8', 
      //send data as part of body 
      data: {designation: designation, deptname: deptname} 
      success: function (data) { alert("posted") }, 
      error: function() { alert('error'); } 
     }); 
}); 

關於API控制器看起來像這樣:

[HttpPost] 
public HttpResponseMessage PostEmployee(String deptname, String designation) 
{ 

} 
+0

情況並非一定如此。沒有理由一個POST不能包含一些在URI中的數據和一些在POST正文中的數據。 –

相關問題