I am using ASP.NET MVC 6 Web API. In the View I use free-jqgrid. Let's borrow Oleg's free jqgrid data to demonstrate my purpose. We already have the table shown.
Next I am going to add new Vendor. Please notify that there is primary key id(identity column) in the database. We don't want it displaying in the screen.
In VendorRespository.cs
, I add the new Vendor as
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();
}
}
My questions:
Not sure the script like?
<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') );
In the controller, not sure what is the code?
// 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; }
My code has many compiling errors such as
'HttpRequest' does not contain a definition for 'CreateResponse' and the best extension method overload 'HttpRequestMessageExtensions.CreateResponse(HttpRequestMessage, HttpStatusCode, Vendor)' requires a receiver of type 'HttpRequestMessage'
Please help me to get rid of the error and inappropriate code.
EDIT:
I borrowed the code snippet from here.