gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:20:48 26 4
gpt4 key购买 nike

我正在尝试将一串对象从我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com