javascript - 无法将对象值从 javascript 传递到 Controller

标签 javascript arrays string model-view-controller javascript-objects

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

我希望它传递的是 ContactID、Address、ContactName 和 Phone。当我调试时,我可以看到它正在传递/获取数据,但它没有传递到 Controller 。我尝试过执行 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.");
                }
            }
        }
    });
}

Controller :

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

添加类

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

最佳答案

您将 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# 模型。

关于javascript - 无法将对象值从 javascript 传递到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29153370/

相关文章:

java - PrintWriter[] 数组中的 NullPointerException - Java

php 仅从 csv 文件中选择第一列、中间列和最后一列

ios - segue 没有在 segue 中传输字符串(swift4)

c# - 字符串 s[s.Length-1] 与 s.Last()

Javascript 用 <NewWord> 替换字符串返回空

Javascript 在传递的术语上隐藏/显示 div

javascript - 如何按字母顺序对下拉菜单项进行排序?

C++显示一维数组

c# - 使用 C# 使用字符数组修剪字符串

javascript - 如何在 asp.net 中更改鼠标悬停时的标签文本