开发者

Model Binder in ASP.Net MVC 3 is not working

开发者 https://www.devze.com 2023-04-10 15:11 出处:网络
I am having a problem to bind the data sent with the model. What I would like to bind is the OrderStatus that even if I pass it 10 it is always turned into 0.

I am having a problem to bind the data sent with the model. What I would like to bind is the OrderStatus that even if I pass it 10 it is always turned into 0. I don't know why. I made this part bold to make it easier to detect in code:

Data sent as 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}]}

The method signature:

 [HttpPost]
 public JsonResult SaveOrUpdateOrderLines( CustomerOrderModel customerOrderModel )

and the model:

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; }

}

Customer Order List Model:

   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; }


    }

Customer Order Header Model:

 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
    }


At first glance the problem I see is that you are sending "OrderStatusName", but that property only has a get function and not a set.

I haven't examined the rest of the code or tested this; but if you don't have a SET for OrderStatusName, it won't work.

You could either send in the OrderStates (which has both get; set;), or implement the set to the OrderStatusName, which I wouldn't recommend as is not good practice.

Hope this helps, -covo

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号