【发布时间】:2016-04-24 02:57:11
【问题描述】:
我正在使用 ASP.NET MVC 6 Web API。在视图中我使用free-jqgrid. 让我们借用Oleg's free jqgrid data 来证明我的目的。我们已经显示了表格。
接下来我要添加新的供应商。请通知数据库中有主键id(标识列)。我们不希望它显示在屏幕上。
在VendorRespository.cs 中,我将新供应商添加为
public void AddVendor(Vendor item)
{
using (VendorDataContext dataContext = new VendorDataContext())
{
dataContext.Database.Connection.ConnectionString = DBUtility.GetSharedConnectionString(
"http://centralized.admin.test.com");
var newVendor = dataContext.Vendors.Create();
newVendor.Company = item.Company;
newVendor.ContactName = item.ContactName;
newVendor.ContactPhone = item.ContactName;
newVendor.UserName = item.UserName;
newVendor.UserKey = item.UserKey;
newVendor.Active = item.Active;
newVendor.FacilityId =item.FacilityId;
newVendor.ClientID = item.ClientID;
dataContext.SaveChanges();
}
}
我的问题:
-
不确定脚本喜欢什么?
<script> API_URL = "/VendorManagement/"; function updateDialog(action) { return { url: API_URL , closeAfterAdd: true , closeAfterEdit: true , afterShowForm: function (formId) { } , modal: true , onclickSubmit: function (params) { var list = $("#jqgrid"); var selectedRow = list.getGridParam("selrow"); rowData = list.getRowData(selectedRow); params.url += rowData.Id; params.mtype = action; } , width: "300" }; } jQuery("#jqgrid").jqGrid('navGrid', { add: true, edit: true, del: true }, updateDialog('PUT'), updateDialog('POST'), updateDialog('DELETE') ); -
在控制器中,不知道代码是什么?
// POST public HttpResponseMessage PostVendor(Vendor item) { _vendorRespository.AddVendor(item); var response = Request.CreateResponse<Vendor>(HttpStatusCode.Created, item); string uri = Url.Link("DefaultApi", new { id = item.Id }); response.Headers.Location = new Uri(uri); return response; }我的代码有很多编译错误如
“HttpRequest”不包含“CreateResponse”的定义,并且最佳扩展方法重载“HttpRequestMessageExtensions.CreateResponse(HttpRequestMessage, HttpStatusCode, Vendor)”需要“HttpRequestMessage”类型的接收器
请帮助我摆脱错误和不适当的代码。
编辑:
我从here.借用了代码sn-p
【问题讨论】:
-
@techspider,是的。我使用
Controller而不是ApiController。那么如何更改我的代码呢? -
尝试改变你的类继承
-
@techspider,我改了。我的代码是从techbrij.com/add-edit-delete-jqgrid-asp-net-web-api 借来的。我还有什么需要改变的?
-
@Love:如果您使用创建数据库表的 SQL 脚本准备演示会更好。你可以将演示项目上传到某个地方(例如在 GitHub 上),我会修改它。
标签: asp.net asp.net-web-api jqgrid