我正在尝试将一串对象从我的javascript传递给控制器。但是当我运行代码时,没有任何内容传递给控制器,我得到的只是一个空值。我经历了调试,我可以看到应该传递的数据字符串,但它永远不会进入或击中控制器。这是我的代码如下。
我希望它传递的是ContactID,Address,ContactName和Phone。当我调试时,我可以看到它传递/获取数据,但它没有传递给控制器。我试过做一个字符串[] 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);
};
}
添加课程
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; }
}
答案 0 :(得分:2)
您将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#模型。
答案 1 :(得分:0)
它可能发生的原因是您没有将属性[HttpPost]
设置为您的控制器。