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.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • for errors, check http://stackoverflow.com/questions/24288426/system-web-httprequestbase-does-not-contain-a-definition-for-createresponse – techspider Jan 18 '16 at 20:08
  • @techspider, yes. I use `Controller` instead of `ApiController`. So how to change my code then? –  Jan 18 '16 at 20:10
  • try change your inheritance of the class – techspider Jan 18 '16 at 20:12
  • @techspider, I changed it. And my code is borrowing from http://techbrij.com/add-edit-delete-jqgrid-asp-net-web-api. What else I have to change? –  Jan 18 '16 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. – Oleg Jan 19 '16 at 06:25
  • @Love: Try `CreatedAtRoute` . See [the answer](http://stackoverflow.com/a/23417612/315935) – Oleg Jan 19 '16 at 06:32
  • @Olg, thanks for your nice help. Based on [tutorial](http://techbrij.com/add-edit-delete-jqgrid-asp-net-web-api) and [web api](http://www.asp.net/web-api/overview/older-versions/creating-a-web-api-that-supports-crud-operations), I created a demo at [onedrive](https://onedrive.live.com/?id=3A8BB9EAEEFF1DB%214159&cid=03A8BB9EAEEFF1DB), the zip file's name is `ASP.NET Web ...`. Because I am not strong on ASP.NET MVC, the project still has some flaws, now the view is not showing but it is close to the working one. I add a 'UserKey' byte array. Please help me to figure out what is wrong. –  Jan 20 '16 at 14:22

1 Answers1

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;
    }