Asp.net mvc ASP.Net MVC 3中的模型绑定器不工作
我在绑定随模型发送的数据时遇到问题。 我想绑定的是OrderStatus,即使我将其传递到10,它也始终会变为0。 我不知道为什么。我将此部分加粗,以便于在代码中检测: 以JSON格式发送的数据:Asp.net mvc ASP.Net MVC 3中的模型绑定器不工作,asp.net-mvc,binding,model,Asp.net Mvc,Binding,Model,我在绑定随模型发送的数据时遇到问题。 我想绑定的是OrderStatus,即使我将其传递到10,它也始终会变为0。 我不知道为什么。我将此部分加粗,以便于在代码中检测: 以JSON格式发送的数据: {"CustomerOrderHeader":{"OrderNumber":"5", "PartnerName":"BLI", "Reference":"", "ShippingDate":"10/152011", "OrderStatusName":"Approve
{"CustomerOrderHeader":{"OrderNumber":"5",
"PartnerName":"BLI",
"Reference":"",
"ShippingDate":"10/152011",
"OrderStatusName":"Approved",
"CustomerOrderHeader.OrderStatus":"10",
"TotalAmount":"16.00",
"CustomerOrderHeader.CurrencyId":"0",
"CustomerOrderHeader.LanguageId":"0",
"DaLine1":"",
"DaLine2":"",
"DaCity":"",
"DaPostalCode":"",
"DaState":"",
"DaCountry":"",
"BaLine1":"",
"BaLine2":"",
"BaCity":"",
"BaPostalCode":"",
"BaState":"",
"BaCountry":"",
"Notes":""},
"CustomerOrderLines":[{"Id":214,"Num":10,"PartNumber":"dasdasd","Description":"dadadasd","OrderedQty":4,"UoM":"inteee","SalesPrice":null,"Amount":16,"LineStatus":10,"ConfirmedShippingDate":null,"ReservedQty":0,"DeliveredQty":0,"RemainingQty":4}]}
方法签名:
[HttpPost]
public JsonResult SaveOrUpdateOrderLines( CustomerOrderModel customerOrderModel )
模型:
public class CustomerOrderModel
{
public CustomerOrderModel()
{
this.CustomerOrderLines = new List<CustomerOrderLineModel>();
this.CustomerOrderHeader = new CustomerOrderHeaderModel();
}
public CustomerOrderHeaderModel CustomerOrderHeader
{ get; set; }
public List<CustomerOrderLineModel> CustomerOrderLines
{ get; set; }
}
客户订单标题模型:
public class CustomerOrderHeaderModel
{
public class CustomDropDown
{
public CustomDropDown(object dataValue, string displayValue)
{
this.DataValue = dataValue;
this.DisplayValue = displayValue;
}
public object DataValue { get; set; }
public string DisplayValue { get; set; }
}
public CustomerOrderHeaderModel()
{
this.CurrencyId = 0;
this.TotalAmount = 0;
this.LanguageId = 0;
this.LanguageTypeList = new List<CustomDropDown>
{
new CustomDropDown(0, "English (Local Language)")
};
this.CurrencyTypeList = new List<CustomDropDown>
{
new CustomDropDown(0, "EUR (Local Currency)")
};
}
//public CustomerOrderHeaderModel(int orderStatus, long totalAmount)
//{
// this.OrderStatus = orderStatus;
// this.TotalAmount = totalAmount;
//}
#region Implementation of ICustomerOrderHeader
//public long CompanyId
//{ get; set; }
public string BillingAddress
{ get; set; }
public string ShippingAddress
{ get; set; }
[DisplayName("Customer name*:")]
[Required]
public string PartnerName
//public string CustomerName
{ get; set; }
[DisplayName("Order Number :")]
public long? OrderNumber
{ get; set; }
[DisplayName("Order Status :")]
public int OrderStatus
{ get; set; }
public string OrderStatusName
{ get
{
switch (OrderStatus)
{
case 0:
return "Draft";
case 10:
return "Approved";
case 20:
return "Ready for shipping";
case 30:
return "In Progress";
case 40:
return "Delivered";
case 50:
return "Closed";
case 60:
return "Cancelled";
default:
return "Undefinied";
}
}
}
[DisplayName("Shipping Date :")]
//[Range(typeof(DateTime), "01/01/1753", "12/31/9999", ErrorMessage = "Value for {0} must be between {1} and {2}")]
[DateRange]
//[DataType(DataType.Date, ErrorMessage = "Please enter a valid date in the format dd/mm/yyyy")]
//[Range(typeof(DateTime), "01/01/1753", "12/31/9999", ErrorMessage = "Please enter a valid date in the format dd/mm/yyyy")]
public DateTime? ShippingDate
{ get; set; }
[DisplayName("Reference :")]
public string Reference
{ get; set; }
public Partner Partner
{ get; set; }
[DisplayName("Description :")]
public string Description
{ get; set; }
[DisplayName("Address Line 1 :")]
public string AddressLine1
{ get; set; }
[DisplayName("Address Line 2 :")]
public string AddressLine2
{ get; set; }
[DisplayName("City :")]
public string City
{ get; set; }
[DisplayName("Postal Code :")]
public string PostalCode
{ get; set; }
[DisplayName("State :")]
public string State
{ get; set; }
[DisplayName("Country :")]
public string Country
{ get; set; }
public bool IsDefaultda
{ get; set; }
public bool IsDefaultba
{ get; set; }
[DisplayName("Currency :")]
public long CurrencyId
{ get; set; }
[DisplayName("Language :")]
public long LanguageId
{ get; set; }
[DisplayName("Total Amount :")]
public decimal TotalAmount
{ get; set; }
[DisplayName("Address Line 1 :")]
public string DaLine1
{ get; set; }
[DisplayName("Address Line 2 :")]
public string DaLine2
{ get; set; }
[DisplayName("City :")]
public string DaCity
{ get; set; }
[DisplayName("Postal Code :")]
public string DaPostalCode
{ get; set; }
[DisplayName("State :")]
public string DaState
{ get; set; }
[DisplayName("Country :")]
public string DaCountry
{ get; set; }
[DisplayName("Address Line 1 :")]
public string BaLine1
{ get; set; }
[DisplayName("Address Line 2 :")]
public string BaLine2
{ get; set; }
[DisplayName("City :")]
public string BaCity
{ get; set; }
[DisplayName("Postal Code :")]
public string BaPostalCode
{ get; set; }
[DisplayName("State :")]
public string BaState
{ get; set; }
[DisplayName("Country :")]
public string BaCountry
{ get; set; }
[DisplayName("Notes :")]
public string Notes
{ get; set; }
public List<CustomDropDown> CurrencyTypeList { get; set; }
public List<CustomDropDown> LanguageTypeList { get; set; }
public List<CustomDropDown> OrderStatusTypeList { get; set; }
#endregion
}
乍一看,我看到的问题是您正在发送OrderStatusName,但该属性只有一个get函数,而没有set 我没有检查代码的其余部分,也没有测试它;但是如果您没有OrderStatusName的设置,它将无法工作 您可以发送包含两个get的OrderStates;设置或者实现OrderStatusName的设置,我不建议这样做,因为这不是一个好的做法 希望这有帮助, -科沃
public class CustomerOrderHeaderModel
{
public class CustomDropDown
{
public CustomDropDown(object dataValue, string displayValue)
{
this.DataValue = dataValue;
this.DisplayValue = displayValue;
}
public object DataValue { get; set; }
public string DisplayValue { get; set; }
}
public CustomerOrderHeaderModel()
{
this.CurrencyId = 0;
this.TotalAmount = 0;
this.LanguageId = 0;
this.LanguageTypeList = new List<CustomDropDown>
{
new CustomDropDown(0, "English (Local Language)")
};
this.CurrencyTypeList = new List<CustomDropDown>
{
new CustomDropDown(0, "EUR (Local Currency)")
};
}
//public CustomerOrderHeaderModel(int orderStatus, long totalAmount)
//{
// this.OrderStatus = orderStatus;
// this.TotalAmount = totalAmount;
//}
#region Implementation of ICustomerOrderHeader
//public long CompanyId
//{ get; set; }
public string BillingAddress
{ get; set; }
public string ShippingAddress
{ get; set; }
[DisplayName("Customer name*:")]
[Required]
public string PartnerName
//public string CustomerName
{ get; set; }
[DisplayName("Order Number :")]
public long? OrderNumber
{ get; set; }
[DisplayName("Order Status :")]
public int OrderStatus
{ get; set; }
public string OrderStatusName
{ get
{
switch (OrderStatus)
{
case 0:
return "Draft";
case 10:
return "Approved";
case 20:
return "Ready for shipping";
case 30:
return "In Progress";
case 40:
return "Delivered";
case 50:
return "Closed";
case 60:
return "Cancelled";
default:
return "Undefinied";
}
}
}
[DisplayName("Shipping Date :")]
//[Range(typeof(DateTime), "01/01/1753", "12/31/9999", ErrorMessage = "Value for {0} must be between {1} and {2}")]
[DateRange]
//[DataType(DataType.Date, ErrorMessage = "Please enter a valid date in the format dd/mm/yyyy")]
//[Range(typeof(DateTime), "01/01/1753", "12/31/9999", ErrorMessage = "Please enter a valid date in the format dd/mm/yyyy")]
public DateTime? ShippingDate
{ get; set; }
[DisplayName("Reference :")]
public string Reference
{ get; set; }
public Partner Partner
{ get; set; }
[DisplayName("Description :")]
public string Description
{ get; set; }
[DisplayName("Address Line 1 :")]
public string AddressLine1
{ get; set; }
[DisplayName("Address Line 2 :")]
public string AddressLine2
{ get; set; }
[DisplayName("City :")]
public string City
{ get; set; }
[DisplayName("Postal Code :")]
public string PostalCode
{ get; set; }
[DisplayName("State :")]
public string State
{ get; set; }
[DisplayName("Country :")]
public string Country
{ get; set; }
public bool IsDefaultda
{ get; set; }
public bool IsDefaultba
{ get; set; }
[DisplayName("Currency :")]
public long CurrencyId
{ get; set; }
[DisplayName("Language :")]
public long LanguageId
{ get; set; }
[DisplayName("Total Amount :")]
public decimal TotalAmount
{ get; set; }
[DisplayName("Address Line 1 :")]
public string DaLine1
{ get; set; }
[DisplayName("Address Line 2 :")]
public string DaLine2
{ get; set; }
[DisplayName("City :")]
public string DaCity
{ get; set; }
[DisplayName("Postal Code :")]
public string DaPostalCode
{ get; set; }
[DisplayName("State :")]
public string DaState
{ get; set; }
[DisplayName("Country :")]
public string DaCountry
{ get; set; }
[DisplayName("Address Line 1 :")]
public string BaLine1
{ get; set; }
[DisplayName("Address Line 2 :")]
public string BaLine2
{ get; set; }
[DisplayName("City :")]
public string BaCity
{ get; set; }
[DisplayName("Postal Code :")]
public string BaPostalCode
{ get; set; }
[DisplayName("State :")]
public string BaState
{ get; set; }
[DisplayName("Country :")]
public string BaCountry
{ get; set; }
[DisplayName("Notes :")]
public string Notes
{ get; set; }
public List<CustomDropDown> CurrencyTypeList { get; set; }
public List<CustomDropDown> LanguageTypeList { get; set; }
public List<CustomDropDown> OrderStatusTypeList { get; set; }
#endregion
}