public static object PerformBindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var idName = string.IsNullOrEmpty(bindingContext.ModelName) ? "id" : bindingContext.ModelName; var valueProviderResult = bindingContext.GetValue(idName); if (valueProviderResult == null) { return null; } var rawId = valueProviderResult.ConvertTo(typeof(string)) as string; var parseResult = HiveId.TryParse(rawId); if (parseResult.Success) { //add the bound value to model state if it's not already there, generally only simple props will be there if (!bindingContext.ModelState.ContainsKey(idName)) { bindingContext.ModelState.Add(idName, new ModelState()); bindingContext.ModelState.SetModelValue(idName, new ValueProviderResult(parseResult.Result, parseResult.Result.ToString(), null)); } } return parseResult.Result; }
/// <summary> /// Binds the model /// </summary> /// <param name="controllerContext"></param> /// <param name="bindingContext"></param> /// <returns></returns> public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var idName = string.IsNullOrEmpty(bindingContext.ModelName) ? "id" : bindingContext.ModelName; var valueProviderResult = bindingContext.GetValue(idName); if (valueProviderResult == null) { return null; } var rawId = valueProviderResult.ConvertTo(typeof(string)) as string; HiveEntityUri nodeId = null; if (HiveEntityUri.TryParse(rawId, out nodeId)) { //add the bound value to model state if it's not already there, generally only simple props will be there if (!bindingContext.ModelState.ContainsKey(idName)) { bindingContext.ModelState.Add(idName, new ModelState()); bindingContext.ModelState.SetModelValue(idName, new ValueProviderResult(nodeId, nodeId.ToString(), null)); } } return nodeId; }
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var models = new List<CheckBoxListViewModel>(); var formKeys = controllerContext.HttpContext.Request.Form.AllKeys.ToArray(); var rootItems = formKeys.Where(s => s.StartsWith("hdrTitle")).ToList(); if (rootItems.Count == 0) return null; foreach (var item in rootItems) { var hdrValue = item.Split('_')[1]; var txtValues = formKeys.Where(s => s.StartsWith("lblLabel_" + hdrValue)).ToArray(); var valValues = formKeys.Where(s => s.StartsWith("valValue_" + hdrValue)).ToArray(); var hdnValues = formKeys.Where(s => s.StartsWith("hdnChk_" + hdrValue)).ToArray(); var model = new CheckBoxListViewModel { HeaderText = Regex.Replace(hdrValue, "([a-z])([A-Z])", "$1 $2"), Items = new List<CheckBoxListItem>() }; for (var index = 0; index < txtValues.Count(); index++) { var listItem = new CheckBoxListItem { Text = bindingContext.GetValue(txtValues[index]), Value = bindingContext.GetValue(valValues[index]), IsChecked = bool.Parse(bindingContext.GetValue(hdnValues[index])) }; model.Items.Add(listItem); } models.Add(model); } return rootItems.Count == 1 ? (object) models.First() : models; }
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { List<CheckBoxListViewModel> models = new List<CheckBoxListViewModel>(); string[] formKeys = controllerContext.HttpContext.Request.Form.AllKeys.ToArray(); List<string> rootItems = formKeys.Where(s => s.StartsWith("hdrTitle")).ToList(); foreach (var item in rootItems) { string hdrValue = item.Split('_')[1]; string[] txtValues = formKeys.Where(s => s.StartsWith("lblLabel_" + hdrValue)).ToArray(); string[] valValues = formKeys.Where(s => s.StartsWith("valValue_" + hdrValue)).ToArray(); string[] hdnValues = formKeys.Where(s => s.StartsWith("hdnChk_" + hdrValue)).ToArray(); CheckBoxListViewModel model = new CheckBoxListViewModel(); model.HeaderText = Regex.Replace(hdrValue, "([a-z])([A-Z])", "$1 $2"); model.Items = new List<CheckBoxListItem>(); for (int index = 0; index < txtValues.Count(); index++) { CheckBoxListItem _item = new CheckBoxListItem(); _item.Text = bindingContext.GetValue(txtValues[index]); _item.Value = bindingContext.GetValue(valValues[index]); _item.IsChecked = bool.Parse(bindingContext.GetValue(hdnValues[index])); model.Items.Add(_item); } models.Add(model); } if (rootItems.Count == 1) return models.First(); else return models; }
/// <summary> /// Binds a datatable param object from the binding context /// </summary> /// <param name="controllerContext"> /// The controller context. /// </param> /// <param name="bindingContext"> /// The binding context. /// </param> /// <returns> /// An instance of datatable param /// </returns> /// <exception cref="ArgumentNullException"> /// If the controllerContext or bindingContext is null, this method will fail /// </exception> public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (controllerContext == null) { throw new ArgumentNullException("controllerContext", "controllerContext is null."); } if (bindingContext == null) { throw new ArgumentNullException("bindingContext", "bindingContext is null."); } var param = new DatatableParam { DatatableId = bindingContext.GetValue<string>("datatableId"), DisplayStart = bindingContext.GetValue<int>("iDisplayStart"), DisplayLength = bindingContext.GetValue<int>("iDisplayLength"), ColumnsCount = bindingContext.GetValue<int>("iColumns"), GlobalSearch = bindingContext.GetValue<string>("sSearch"), SortingColumnsCount = bindingContext.GetValue<int>("iSortingCols"), Echo = bindingContext.GetValue<string>("sEcho") }; param.Searchable = new bool[param.ColumnsCount]; param.Search = new string[param.ColumnsCount]; param.Regex = new bool[param.ColumnsCount]; param.Sortable = new bool[param.ColumnsCount]; param.DataProperties = new string[param.ColumnsCount]; param.SortingColumns = new int[param.ColumnsCount]; param.SortDirections = new string[param.ColumnsCount]; for (int i = 0; i < param.ColumnsCount; i++) { param.Searchable[i] = bindingContext.GetValue<bool>("bSearchable_" + i); param.Search[i] = bindingContext.GetValue<string>("sSearch_" + i); param.Regex[i] = bindingContext.GetValue<bool>("bRegex_" + i); param.Sortable[i] = bindingContext.GetValue<bool>("bSortable_" + i); param.DataProperties[i] = bindingContext.GetValue<string>("mDataProp_" + i); param.SortingColumns[i] = bindingContext.GetValue<int>("iSortCol_" + i); param.SortDirections[i] = bindingContext.GetValue<string>("sSortDir_" + i); } return param; }
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var postModel = new PostViewModel { Post = new PostEntity { PostID = int.Parse(bindingContext.GetValue("Post.PostID")), PostTitle = bindingContext.GetValue("Post.PostTitle"), PostContent = bindingContext.GetValue("Post.PostContent"), PostUrl = bindingContext.GetValue("Post.PostUrl"), PostAddedDate = DateTime.Parse(bindingContext.GetValue("Post.PostAddedDate")), UserCanAddComments = bool.Parse(bindingContext.GetValue("Post.UserCanAddComments")), CanBeShared = bool.Parse(bindingContext.GetValue("Post.CanBeShared")), IsPrivate = bool.Parse(bindingContext.GetValue("Post.IsPrivate")), EntryType = byte.Parse(bindingContext.GetValue("Post.EntryType")) } }; postModel.Post.Order = postModel.Post.EntryType == 2 ? (int?)GetOrder(bindingContext.GetValue("Post.Order")) : null; IModelBinder ckBinder = new CheckBoxListViewModelBinder(); postModel.Categories = (CheckBoxListViewModel)ckBinder.BindModel(controllerContext, bindingContext); if (postModel.Post.EntryType == 1) { if (!postModel.Categories.Items.Any(c => c.IsChecked)) { var general = postModel.Categories.Items.SingleOrDefault(c => c.Value == "1"); if (general != null) { general.IsChecked = true; } } postModel.Tags = bindingContext.GetValue("hdnAddedTags"); } return postModel; }