json - 使用 Ajax 将复杂对象传递给 MVC 中的控制器
问题描述
我正在尝试通过 ajax 将一组复杂对象传递给控制器。使用网络调试器,我可以看到数据正在传递给控制器,但是参数没有正确填充。
我的Javascript如下:
function objectifyForm(formArray) {
var returnArray = {};
for (var i = 0; i < formArray.length; i++) {
returnArray[formArray[i]['name']] = formArray[i]['value'];
}
return returnArray;
}
function ContractPost() {
var businessInfoForm = objectifyForm($("#form-business-info").serializeArray());
var gsvForm = objectifyForm($("#form-contract-gsv").serializeArray());
var BusinessInfo = JSON.stringify(businessInfoForm);
var StreetViewContract = JSON.stringify(gsvForm);
var vmData = [{
BusinessInfo,
StreetViewContract
}];
var dataToSend = JSON.stringify({ 'viewModel':vmData[0] });
$.ajax({
type: 'POST',
url: "@Url.Content("~/Bundle/ContractPost")",
data: dataToSend,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
//businessInfo.html(data);
alert(data.responseText);
}
});
}
我的控制器如下:
[HttpPost]
public ActionResult ContractPost(ContractBundleModel viewModel)
{
...
}
从网络传递的数据如下:
如您所见,参数中的 viewModel 对象未正确填充:
我的请求有效负载如下:
{"viewModel":{"BusinessInfo":"{\"business_name\":\"Megan's Test Text\",\"address1\":\"915 E Lincoln Hwy\",\"address2\":\"\",\"city\":\"Jacksonville Beach\",\"state\":\"FL\",\"zip\":\"32250\",\"email\":\"megan.moser@ampcorporate.com\",\"website\":\"\",\"contact\":\"Megan Moser\",\"phone1\":\"8157876355\",\"mobile\":\"\",\"business_category\":\"\",\"AptDate\":\"\",\"AptTime\":\"\",\"appointment_tbd\":\"false\"}","StreetViewContract":"{\"business_name\":\"\",\"address1\":\"\",\"address2\":\"\",\"city\":\"\",\"state\":\"AL\",\"zip\":\"\",\"email\":\"\",\"website\":\"\",\"contact\":\"\",\"phone1\":\"\",\"mobile\":\"\",\"business_category\":\"\",\"AptDate\":\"\",\"AptTime\":\"\",\"appointment_tbd\":\"false\",\"program_id\":\"4\",\"fee\":\"0\",\"discount\":\"0\",\"tax_exempt\":\"false\",\"sales_tax\":\"0\",\"grand_total\":\"0\",\"contract_number\":\"\",\"customer_number\":\"\",\"rep.empid\":\"\"}"}}
我的视图模型如下:
public class ContractBundleModel
{
public contract BusinessInfo { get; set; }
public contract PrintContract { get; set; }
public contract OptimaContract { get; set; }
public contract StreetViewContract { get; set; }
public contract TextContract { get; set; }
}
...和一个合同(从 EntityFramework 生成)对象如下:
public class contract
{
public contract();
[MaxLength(500)]
public string address1 { get; set; }
[MaxLength(500)]
public string address2 { get; set; }
[MaxLength(200)]
public string address_alt { get; set; }
public DateTime? appointment_date { get; set; }
public bool appointment_tbd { get; set; }
[MaxLength(100)]
public string business_category { get; set; }
[MaxLength(100)]
public string business_category2 { get; set; }
[MaxLength(100)]
public string business_category3 { get; set; }
[MaxLength(100)]
public string business_category4 { get; set; }
[MaxLength(100)]
public string business_category5 { get; set; }
[MaxLength(100)]
public string business_category6 { get; set; }
[MaxLength(100)]
public string business_category7 { get; set; }
[MaxLength(100)]
public string business_category8 { get; set; }
[MaxLength(500)]
public string business_name { get; set; }
[MaxLength(500)]
public string city { get; set; }
[MaxLength(100)]
public string city_alt { get; set; }
public DateTime? completed { get; set; }
[MaxLength(500)]
public string contact { get; set; }
public int? contract_number { get; set; }
public DateTime created { get; set; }
[MaxLength(100)]
public string created_by { get; set; }
[MaxLength(500)]
public string created_note { get; set; }
public int? customer_number { get; set; }
public DateTime? deleted { get; set; }
[MaxLength(100)]
public string deleted_by { get; set; }
[MaxLength(500)]
public string deleted_note { get; set; }
public double discount { get; set; }
public bool discount_print_opt { get; set; }
public int? doc_id { get; set; }
public int doc_set_id { get; set; }
[MaxLength(500)]
public string email { get; set; }
[MaxLength(20)]
public string existing_customer_opt { get; set; }
[MaxLength(20)]
public string fax { get; set; }
public double fee { get; set; }
public double grand_total { get; set; }
public double gsv_delivery_fee { get; set; }
[MaxLength(100)]
public string gsv_delivery_option { get; set; }
public int id { get; set; }
public bool inside_sale { get; set; }
[MaxLength(20)]
public string lead { get; set; }
[MaxLength(20)]
public string mobile { get; set; }
public DateTime? modified { get; set; }
[MaxLength(100)]
public string modified_by { get; set; }
[MaxLength(500)]
public string modified_note { get; set; }
public DateTime? notification_sent { get; set; }
[MaxLength(20)]
public string phone1 { get; set; }
[MaxLength(20)]
public string phone2 { get; set; }
public bool previous_contract { get; set; }
public int? previous_contract_number { get; set; }
public bool print_attachment_camera_ready { get; set; }
public bool print_attachment_email { get; set; }
[MaxLength(20)]
public string print_attachment_opt { get; set; }
public double print_bcat_price1 { get; set; }
public double print_bcat_price2 { get; set; }
public double print_bcat_price_each1 { get; set; }
public double print_bcat_price_each2 { get; set; }
public int print_bcat_quantity1 { get; set; }
public int print_bcat_quantity2 { get; set; }
[MaxLength(20)]
public string print_builder_ad_size_opt { get; set; }
public double print_builder_price1 { get; set; }
public double print_builder_price2 { get; set; }
public double print_builder_price_each1 { get; set; }
public double print_builder_price_each2 { get; set; }
public int print_builder_quantity1 { get; set; }
public int print_builder_quantity2 { get; set; }
[MaxLength(20)]
public string print_builder_special_offer1 { get; set; }
[MaxLength(20)]
public string print_builder_special_offer2 { get; set; }
[MaxLength(20)]
public string print_builder_special_offer_opt { get; set; }
[MaxLength(20)]
public string print_bw_page1_opt { get; set; }
[MaxLength(20)]
public string print_bw_page2_opt { get; set; }
[MaxLength(20)]
public string print_bw_page3_opt { get; set; }
[MaxLength(20)]
public string print_bw_page4_opt { get; set; }
public double print_bw_price { get; set; }
public bool print_bw_section_sponsor { get; set; }
public bool print_bw_special_placement { get; set; }
public bool print_bw_wp_sponsor { get; set; }
[MaxLength(20)]
public string print_cat_opt { get; set; }
public double print_cat_price { get; set; }
[MaxLength(20)]
public string print_color_banner_opt { get; set; }
[MaxLength(20)]
public string print_color_bic_opt { get; set; }
[MaxLength(20)]
public string print_color_boc_opt { get; set; }
[MaxLength(20)]
public string print_color_fic_opt { get; set; }
[MaxLength(20)]
public string print_color_foc_opt { get; set; }
[MaxLength(20)]
public string print_color_insert_opt { get; set; }
public double print_color_price { get; set; }
public bool print_color_special_placement { get; set; }
public bool print_color_spine { get; set; }
public string print_directories { get; set; }
[MaxLength(20)]
public string print_display_ad_type_opt { get; set; }
[MaxLength(20)]
public string print_expedite_opt { get; set; }
[MaxLength(500)]
public string print_listing1 { get; set; }
[MaxLength(500)]
public string print_listing2 { get; set; }
[MaxLength(20)]
public string print_renewal_opt { get; set; }
public double print_terms_full_fee { get; set; }
[MaxLength(20)]
public string print_wp_emphasis_opt { get; set; }
public int product_id { get; set; }
public int program_id { get; set; }
[NotMapped]
public rep rep { get; set; }
public int rep_id { get; set; }
public double sales_tax { get; set; }
public double sales_tax_rate { get; set; }
public int save_version { get; set; }
[MaxLength(20)]
public string state { get; set; }
[MaxLength(20)]
public string state_alt { get; set; }
public DateTime? submitted { get; set; }
public bool subscription_mode { get; set; }
public double subscription_monthly_fee { get; set; }
public double subscription_setup_fee { get; set; }
public bool tax_exempt { get; set; }
public double txt_add_location_fee { get; set; }
public bool txt_add_location_opt { get; set; }
public int txt_add_location_quantity { get; set; }
public bool txt_add_vanity_code { get; set; }
[MaxLength(20)]
public string txt_materials_opt { get; set; }
[MaxLength(200)]
public string txt_vanity_code { get; set; }
public DateTime? validated { get; set; }
[MaxLength(50)]
public string vanity { get; set; }
[MaxLength(500)]
public string website { get; set; }
[MaxLength(20)]
public string zip { get; set; }
[MaxLength(20)]
public string zip_alt { get; set; }
}
解决方案
推荐阅读
- python - scipy 肯定已安装但不会导入
- java - Cucumber-Appium - 如果在 @Before 中初始化驱动程序,则在哪里存储 Hook
- rstudio - 计算搜索词 - rstudio
- c# - 如何搜索列表
与用户输入字符串? - python - 使用 tf.py_func 作为损失函数实现梯度下降
- html - 我无法按下按钮,当我将它们居中时选择框(文本对齐:居中;)
- react-native - 如何在点击反应导航之外的按钮时跳转到根目录
- admin-on-rest - 反应管理员 | 无法使用 FileInput 上传文件
- liquibase - 我的 databaseChangeLog 层次结构有意义吗?
- linux - 如何监控繁忙线程的健康状况?