Json 使用Ajax将复杂对象传递给MVC中的控制器
我试图通过ajax将一组复杂对象传递给控制器。使用网络调试器,我可以看到数据正在传递到控制器,但是参数ise没有正确填充 我的Javascript如下所示:Json 使用Ajax将复杂对象传递给MVC中的控制器,json,ajax,asp.net-mvc,Json,Ajax,Asp.net Mvc,我试图通过ajax将一组复杂对象传递给控制器。使用网络调试器,我可以看到数据正在传递到控制器,但是参数ise没有正确填充 我的Javascript如下所示: function objectifyForm(formArray) { var returnArray = {}; for (var i = 0; i < formArray.length; i++) { returnArray[formArray[i]['name']] = formArray[i]
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":{"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; }
}
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; }
}
从网络传递的数据如下所示:
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":{"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; }
}
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; }
}
如您所见,参数中的viewModel对象未正确填充:
我的请求有效载荷如下:
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":{"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; }
}
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; }
}
我的ViewModel如下所示:
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":{"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; }
}
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; }
}
…而契约(由EntityFramework生成)对象如下所示:
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":{"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; }
}
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; }
}
你试过把[FromBody]放进你的接收控制器吗?我还没有试过那种方法。那么,接收控制器是否需要接受[FromBody]ContractBundleModel viewModel或[FromBody]string viewModel?我决定将视图模型拆分为单个对象并作为参数发送。现在正在填充参数对象。