public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            if (bindingContext.ModelType != typeof(CustomerParameter))
            {
                return(false);
            }

            ValueProviderResult value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

            if (value == null)
            {
                return(false);
            }

            string key = value.RawValue as string;

            if (key == null)
            {
                bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Wrong value type");
                return(false);
            }

            var parameter = CustomerParameter.Parse(key);

            if (parameter != null)
            {
                bindingContext.Model = parameter;
                return(true);
            }

            bindingContext.ModelState.AddModelError(
                bindingContext.ModelName, "Cannot convert value to Customer Parameters");
            return(false);
        }
Пример #2
0
        public static CustomerParameter Parse(string entryParameters)
        {
            var parts = entryParameters.Split(',');
            int countOfCurrentParameters = typeof(CustomerParameter).GetProperties().Length;

            if (parts.Length == 0 || parts.Length < countOfCurrentParameters)
            {
                return(null);
            }

            string customerId       = parts[0].ToStringValueOrEmpty();
            string customerName     = parts[1].ToStringValueOrEmpty();
            string phone            = parts[2].ToStringValueOrEmpty();
            string carLicensePlates = parts[3].ToStringValueOrEmpty();

            CustomerParameter result = new CustomerParameter
            {
                CustomerId       = customerId,
                CustomerName     = customerName,
                CustomerPhone    = phone,
                CarLicensePlates = carLicensePlates
            };

            return(result);
        }