无法将对象值从 javascript 传递到控制器

Not able to Pass object values from javascript to controller

我正在尝试将一串对象从我的 javascript 传递到控制器。但是当我 运行 代码时,没有任何东西传递给控制器​​,我得到的只是一个空值。我进行了调试,我可以看到应该传递的数据字符串,但它永远不会进入或到达控制器。下面是我的代码。

我想让它传递的是 ContactID、Address、ContactName 和 Phone。当我调试时,我可以看到它是 passing/getting 数据,但它没有传递给控制器​​。我试过做一个 string[ ] obj,但我仍然得到一个空引用。我确定我遗漏了一些东西,只需要有人为我指明正确的方向。

提前致谢!

JavaScript:

  function saveEdit(element) {
    var form = $(element).closest('tr');
    model = retrieveEditCustomerModel(form);
    PostObject("Home/Update", model, replacethisEditRow, element);
}
function retrieveEditCustomerModel(form) {
    var model = new Object();
    model.CompanyID = $(form).find('.customer-stored-id').attr('data-value');
    model.CompanyName = $(form).find('#SelectedCustomer_CompanyName').val();
    model.Address = $(form).find('#SelectedCustomer_Address').val();
    model.ContactName = $(form).find('#SelectedCustomer_ContactName').val();
    model.Phone = $(form).find('#SelectedCustomer_Phone').val();
    return model;
}

function replacethisEditRow(result, element) {
    $(element).closest('tr').replaceWith(result);
}


__________________________________________________________________________
function PostObject(url, object, callback, param1, param2, errorCallback) {
    var data = JSON.stringify(object);
    $.ajax({
        url: "../../"+ url ,
        data: data,
        type: "POST",
        dataType: 'json',
        contentType: "application/json",
        success: function (result) {
            if (callback != null && callback != undefined) {
                callback(result, param1, param2);
            }
        },
        error: function (result) {
            if (errorCallback != undefined) {
                errorCallback(result.responseText);
            }
            else {
                if (result.responseText != '') {
                    alert(result.responseText);
                }
                else {
                    alert("An error occurred while processing results.  Please consult an administrator.");
                }
            }
        }
    });
}

控制器:

  public ActionResult Update(CustomersViewModel obj)
        {
            using (CustomerEntities db = new CustomerEntities())
        {


      Customer existing =  db.Customers.Find(obj.SelectedCustomer.CustomerID);

            existing.CompanyName = obj.SelectedCustomer.CompanyName;
            existing.Address = obj.SelectedCustomer.Address;
            existing.ContactName = obj.SelectedCustomer.ContactName;
            existing.Phone = obj.SelectedCustomer.Phone;
            db.SaveChanges();

            CustomersViewModel model = new CustomersViewModel();
           model.Customers = db.Customers.OrderBy(m =>   m.CustomerID).Take(5).ToList();

            model.SelectedCustomer = existing;
            model.DisplayMode = "ReadOnly";
            return View("Index", model);
        };
    }  

添加Class

  public class CustomersViewModel
{
    //This property holds a list of customers to be displayed on the view
    public List<Customer> Customers { get; set; }

    //This property points to a customer that is selected by the user if no customer is selected then it will be null. 
    public Customer SelectedCustomer { get; set; }

    // This property indicates the mode of the customer details area.  Possible values are readonly after selecton.   
    public string DisplayMode { get; set; }
}

发生这种情况的原因是您没有为您的控制器设置属性 [HttpPost]

您将 SelectedCustomer 作为 POST 正文中的整个对象发送。

将您的函数更改为 return { SelectedCustomer: model };

function retrieveEditCustomerModel(form) {
    var model = new Object();
    model.CompanyID = $(form).find('.customer-stored-id').attr('data-value');
    model.CompanyName = $(form).find('#SelectedCustomer_CompanyName').val();
    model.Address = $(form).find('#SelectedCustomer_Address').val();
    model.ContactName = $(form).find('#SelectedCustomer_ContactName').val();
    model.Phone = $(form).find('#SelectedCustomer_Phone').val();
    /* return model; */
    return { SelectedCustomer: model };
}

这将正确填充 C# 模型。