ASP.Net MVC 3中的模型绑定器不起作用

时间:2011-10-05 10:15:09

标签: asp.net-mvc binding model

我遇到了将模型发送的数据绑定的问题。 我想要绑定的是OrderStatus,即使我通过它10它总是变成0。 我不知道为什么。我把这个部分加粗,以便在代码中更容易检测:

以JSON格式发送的数据:

 {"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 CustomerOrderLineModel
    {
        public CustomerOrderLineModel()
        {

        }

        //TODO: Uncomment it
        public long Id  { get; set; }

        [Required]
        public String Num 
        { 
            get; 
            set; 
        }

        //[ExistsOrEmptyPartNum(ErrorMessage = "The Part Number doesn't exist")]
        //[Required]
        [ExistInventoryPartNumOrNullAttribute]
        public String PartNumber { get; set; }

        public String Description { get; set; }


        [Numeric(ErrorMessage = "Should be a  number")]
        [Min(0, ErrorMessage = "The number should be greater than zero")]
        [IsTheSameTypeOfUOM("UnitOfMeasureId")]
        public String OrderedQty { get; set; }


        public String UoM { get; set; }
        public long? UnitOfMeasureId { get; set; }

        [Numeric(ErrorMessage = "Should be a  number")]
        public String SalesPrice { get; set; }

        //[Numeric(ErrorMessage = "Should be a  number")]
        //[Min(0, ErrorMessage = "The number should be greater than zero")]
        public String Amount { get; set; }

        public String LineStatus { get; set; }


        private int? _lineStatusNum;

        public int? LineStatusNum
        {
            get
            {
                if (this.LineStatus == "Draft")
                {
                    _lineStatusNum = 0;
                    return _lineStatusNum;
                }
                _lineStatusNum = null;
                return null;
            }
            set
            {
                if (value == 0)

                {
                   _lineStatusNum  = value;
                    this.LineStatus = "Draft";
                }
                if (value == 10)
                {
                    _lineStatusNum = value;
                    this.LineStatus = "Approved";
                }
            }
        }
        public DateTime? ConfirmedShippingDate { get; set; }

        [Numeric(ErrorMessage = "Should be a  number")]
        public String ReservedQty { get; set; }
        [Numeric(ErrorMessage = "Should be a  number")]
        public String DeliveredQty { get; set; }
        [Numeric(ErrorMessage = "Should be a  number")]
        public String RemainingQty { get; set; }

        public CustomerOrderHeader CustomerOrderHeader { 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
    }

1 个答案:

答案 0 :(得分:0)

乍一看,我看到的问题是您发送的是“OrderStatusName”,但该属性只有获取功能,而不是设置

我没有检查其余的代码或测试过这个;但如果您没有OrderStatusName的SET,它将无效。

您可以发送OrderStates(它同时具有get; set;),或者将该集实现为OrderStatusName,我不建议这是不好的做法。

希望这有帮助, -covo