public object BindModel(ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext) { object result = null; // Don't do this here! // It might do bindingContext.ModelState.AddModelError // and there is no RemoveModelError! // // result = base.BindModel(controllerContext, bindingContext); string modelName = bindingContext.ModelName; string attemptedValue = bindingContext.ValueProvider.GetValue(modelName).AttemptedValue; // Depending on CultureInfo, the NumberDecimalSeparator can be "," or "." // Both "." and "," should be accepted, but aren't. string wantedSeperator = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator; string alternateSeperator = (wantedSeperator == "," ? "." : ","); if (attemptedValue.IndexOf(wantedSeperator) == -1 && attemptedValue.IndexOf(alternateSeperator) != -1) { attemptedValue = attemptedValue.Replace(alternateSeperator, wantedSeperator); } try { if (bindingContext.ModelMetadata.IsNullableValueType && string.IsNullOrWhiteSpace(attemptedValue)) { return(null); } result = double.Parse(attemptedValue, NumberStyles.Any); } catch (FormatException e) { bindingContext.ModelState.AddModelError(modelName, e); } return(result); }
public object BindModel(ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext) { ShoppingCart cart = null; if (controllerContext.HttpContext.Session != null) { cart = (ShoppingCart)controllerContext.HttpContext.Session[sessionKey]; } if (cart == null) { cart = new ShoppingCart(); if (controllerContext.HttpContext.Session != null) { { controllerContext.HttpContext.Session[sessionKey] = cart; } } } return(cart); }
public object BindModel(ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext) { // get the Cart from the session Cart cart = null; if (controllerContext.HttpContext.Session != null) { cart = (Cart)controllerContext.HttpContext.Session[sessionKey]; } // create the Cart if there wasn't one in the session data if (cart == null) { cart = new Cart(); if (controllerContext.HttpContext.Session != null) { controllerContext.HttpContext.Session[sessionKey] = cart; } } // return the cart return(cart); }
public object BindModel(ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext) { var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); return(new ObjectId(valueProviderResult.AttemptedValue)); }