0

C# MVC4 I am receiving an error of:

GET http://BLAHBLAHBLAH/Address/AddressData/ [HTTP/1.1 404 Not Found 71ms]

in the debugger. My JS definition is as follows:

$(function () {

    var filter = $("#ClientId").val();

    //Address Grid
    $("#tickets").jqGrid({
        url: '/Address/AddressData/',
        datatype: 'json',
        mtype: 'GET',
        postData: {
            filters: filter
        },
        closeOnEscape: true,
        multipleSearch: true,
        closeAfterSearch: true,
        colNames: ['Action', 'AddressId', 'AddressLine1', 'AddressLine2', 'City', 'State', 'Zip', 'Available'],
        colModel: [
            {
                name: 'Action',
                width: 220,
                align: 'center',
                sortable: false,
                search: false,
                formatter: EditBtnFormatter,

            },
            {
                name: 'Id',
                index: 'Id',
                width: 300,
                align: 'left',
                sortable: true,
                search: true,

            },
            {
                name: 'AddressLine1',
                index: 'AddressLine1',
                width: 800,
                align: 'left',
                sortable: true,
                search: false,


            },
            {
                name: 'AddressLine2',
                index: 'AddressLine2',
                width: 600,
                align: 'left',
                sortable: true,
                search: false,


            },
            {
                name: 'City',
                index: 'City',
                width: 350,
                align: 'left',
                sortable: true,
                search: false,

            },
            {
                name: 'State',
                index: 'State',
                width: 200,
                align: 'left',
                sortable: true,
                search: false,
            },
            {
                name: 'Zip',
                index: 'Zip',
                width: 250,
                align: 'left',
                sortable: true,
                search: false,
            },
            {
                name: 'Available',
                index: 'Deleted',
                width: 250,
                align: 'left',
                sortable: true,
                search: false,
            }
        ],

        pager: jQuery('#pager'),
        rowNum: 30,
        rowList: [20, 30, 40, 50],
        search: true,
        sortname: 'AddressLine1',
        sortorder: 'asc',
        viewrecords: true,
        gridview: true,
        imgpath: 'content/themes/base/images',
        caption: 'Addresses',
        formatter: 'Action',
        autowidth: true,
        formatoptions: {
            keys: true,
            editformbutton: true
        }
    }).jqGrid('gridResize', null).navGrid('#pager', { view: false, del: false, add: false, edit: false },
        {}, // default settings for edit
        {}, // default settings for add
        {}, // delete
        {
            closeOnEscape: true, multipleSearch: true, searchOnEnter: true,
            closeAfterSearch: true
        }, // search options
        {}
    );
    jQuery("#addrs").setGridWidth(1200, true);
    jQuery("#addrs").setGridHeight(600, true);

    function EditBtnFormatter(cellvalue, options, rowObject) {
        console.log(rowObject);

        return (('<a title="Edit" href ="@Url.Action("Edit","Address")' + '/' + rowObject[1] + '"><img src="../../Content/themes/base/images/edit_icon_fer_checklist.png" /></a>'));
    }

and my controller is

public JsonResult AddressData(string sidx, string sord, int page, int rows, bool _search, string filters)
    {
        var newfil = Int32.Parse(filters);
        var pageSize = rows;
        var totalRecords = (from addrs in db.Addresses where addrs.ClientId == newfil select addrs).Count();
        var totalPages = (int)Math.Ceiling(totalRecords / (float)pageSize);
        string orderby = sidx + " " + sord;
        if (sidx == "AddressLine1")
        {
            orderby = "AddressLine1" + " " + sord;
        }
        var addresses = (from addr in db.Addresses select addr).OrderBy(orderby);

        if (_search)
        {
            addresses = addresses.Where(x => x.ClientId.Equals(newfil));
        }

        var addressData =
            (from addrs in addresses
             select new { addrs.Id, addrs.AddressLine1, addrs.AddressLine2, addrs.City, addrs.State, addrs.Zip, addrs.Deleted }).ToList
                ();
        var jsonData = new
        {
            total = totalPages,
            page,
            records = totalRecords,
            rows = (
                       from item in addressData
                       select new
                       {
                           cell = new[]
                                       {
                                           string.Empty,item.Id.ToString(), item.AddressLine1, item.AddressLine2, item.City, item.State, item.Zip, item.Deleted
                                       }
                       }
                   )
        };
        return Json(jsonData, JsonRequestBehavior.AllowGet);
    }

It works locally, but for some reason it will not populate the grid and I am getting the 404 error even though I know the link is valid. I hope someone can point out just a silly mistake I've overlooked.

Colt Stumpf
  • 121
  • 1
  • 10
  • Could you verify in web browser which *correct* URL should be opened in your solution? If `/Address/AddressData/` (`http://BLAHBLAHBLAH/Address/AddressData/`) is Not Found - it is wrong, then you have probably just add some prefix to the URL. – Oleg Feb 11 '15 at 09:51
  • AddressData is the method that loads the data to the grid on the Address controller. I have jqgrid working on several different pages of this app and literally just copy and pasted from one to the other so i'm just confused. This is on the edit page. on the Index page I have set up the same way where url='/Address/GridData' and it works... – Colt Stumpf Feb 12 '15 at 04:31
  • Also if I use @Url.Action("AddressData") which should use the relative link to the method it still doesnt work. Its like AddressData is not there but as I listed in the controller it is. – Colt Stumpf Feb 12 '15 at 04:40
  • The problem is not that I don't truth you. I want just find the reason of your problem. Do you tried to verify in web browser which correct URL? Typical problem is: you installed your project on the site in some *subfolder*. So the correct URL will be `http://BLAHBLAHBLAH/SiteNameAddress/AddressData/`. I personally use path *relative to the current page*. I use `window.location.pathname` to calculate the `baseUrl` like in [the answer](http://stackoverflow.com/a/2820725/315935). – Oleg Feb 12 '15 at 06:00
  • I attempted this var pathArray = window.location.pathname.split('/'); var rootPath = ''; for (var i = 0; i < pathArray.length; i++) { var p = pathArray[i]; if (p === "") { continue; } if (p.toLowerCase() !== 'edit') { rootPath += '/'; rootPath += p; } else { break; } console.log(rootPath); } $("#tickets").jqGrid({ url: rootPath + '/AddressData', but works locally only – Colt Stumpf Feb 16 '15 at 17:46

1 Answers1

0

When running localhost it typically puts the project name after localhost like this:

http://localhost/projectname/page

When running on a server, you lose the project name:

http://server/page

Make sure your path in jquery realizes that.

Tim Southard
  • 604
  • 4
  • 16
  • http://BLAHBLAHBLAH{localhost is your server from prospective of the application I thought}/Address{controller}/AddressData{method}/ Am I missing something there? – Colt Stumpf Feb 11 '15 at 04:00
  • The 404 is obviously telling you it can't find the page. Try to access the page/method directly from the browser on the server and see if you can find the right path that way, then figure out the difference. – Tim Southard Feb 11 '15 at 04:20