0

I am trying to post a list of JSON objects to a controller, I have been trying to follow this post Passing A List Of Objects Into An MVC Controller Method Using jQuery Ajax However I get an error in the console saying that resource cannot be found even though the action is in the controller and I am passing it the right parameters and so the action method does not get hit on the breakpoint.

[HttpPost]
    public ActionResult EditWidget(List<SaveWidgetModel> widget)
    {
        if (ModelState.IsValid)
        {
            foreach (var item in widget) 
            {
                var targetWidget = db.widgets.Where(EditWidget => EditWidget.WidgetCode == item.id).FirstOrDefault();
                targetWidget.x = item.x;
                targetWidget.y = item.y;
                targetWidget.height = item.height;
                targetWidget.width = item.height;

                db.Entry(targetWidget).State = EntityState.Modified;
                db.SaveChanges();
            }

            return RedirectToAction("Index");
        }
        return new EmptyResult();
    }

and here is my javascript code

function SaveDashboard(){
    var gridArray = _.map($('.grid-stack .grid-stack-item:visible'), function (el) {
        el = $(el);
        var gridID = el.find('.grid-stack-item-content.ui-draggable-handle').first().attr('id');
        var node = el.data('_gridstack_node');
        return {
            id: gridID,
            x: node.x,
            y: node.y,
            width: node.width,
            height: node.height
        };
    });

    gridArray = JSON.stringify({ 'widget' : gridArray});

    $.ajax({
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        url: 'Dashboard/EditWidgets/',
        type: 'POST',
        data: gridArray,
        success: function (dataset) {
        },
        failure: function (xhr, error) {
            console.log(xhr)
            console.log(error)
        },
    });
}
Community
  • 1
  • 1
Johnathon64
  • 1,280
  • 1
  • 20
  • 45

2 Answers2

1

Your controller action is singular

public ActionResult EditWidget()

but your AJAX call is plural

url: 'Dashboard/EditWidgets/

Paul Abbott
  • 7,065
  • 3
  • 27
  • 45
0

This maybe related to your url:

url: 'Dashboard/EditWidget/',

Maybe try

url: '/Dashboard/EditWidget/',

The forward slash tells it to start the search in the root, I think its looking for a folder at the top level

code
  • 1,127
  • 7
  • 14