0

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. newvendor

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:

  1. 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')
    );
    

  2. 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.

7
  • for errors, check stackoverflow.com/questions/24288426/… Commented Jan 18, 2016 at 20:08
  • @techspider, yes. I use Controller instead of ApiController. So how to change my code then? Commented Jan 18, 2016 at 20:10
  • try change your inheritance of the class Commented Jan 18, 2016 at 20:12
  • @techspider, I changed it. And my code is borrowing from techbrij.com/add-edit-delete-jqgrid-asp-net-web-api. What else I have to change? Commented Jan 18, 2016 at 20:15
  • @Love: It would be better if you prepare the demo with the SQL script which creates the database table. You can upload the demo project somewhere (on GitHub for example) and I will modify it. Commented Jan 19, 2016 at 6:25

1 Answer 1

0

I need add the code such as

 [Microsoft.AspNet.Mvc.HttpGet]
    public dynamic GetVendorById(int pkey)
    {

        return null;
    }

And

 // POST
    [System.Web.Http.HttpPost]
    public HttpResponseMessage PostVendor(Vendor item)
    {
        _vendorRespository.AddVendor(item);
        var response = Request.CreateResponse<Vendor>(HttpStatusCode.Created, item);
        string uri = Url.Link("/VendorManagement/GetVendorById", new { id = item.pkey });
        response.Headers.Location = new Uri(uri);
        return response;
    }
Sign up to request clarification or add additional context in comments.

Comments