【发布时间】:2014-01-27 14:15:46
【问题描述】:
我认为有以下cshtml:
<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>
我想将两个下拉列表的值发布到我的 Web 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 控制器发布方法:
public HttpResponseMessage PostEmployee(EmployeeViewModel newEmployee, String deptname, String designation)
{
//code
}
如何将 dropdownlost 的值发送到我的 ApiController。
【问题讨论】:
-
API Controller不能接受这样的参数。添加deptname和designation作为EmployeeViewModel的属性或完全创建新模型。
标签: jquery ajax asp.net-mvc asp.net-mvc-4 asp.net-web-api