当前位置: 首页>>代码示例>>C#>>正文


C# ModelBindingContext.GetValue方法代码示例

本文整理汇总了C#中System.Web.Mvc.ModelBindingContext.GetValue方法的典型用法代码示例。如果您正苦于以下问题:C# ModelBindingContext.GetValue方法的具体用法?C# ModelBindingContext.GetValue怎么用?C# ModelBindingContext.GetValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Web.Mvc.ModelBindingContext的用法示例。


在下文中一共展示了ModelBindingContext.GetValue方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: PerformBindModel

        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;
        }
开发者ID:RebelCMS,项目名称:rebelcmsxu5,代码行数:26,代码来源:HiveIdModelBinder.cs

示例2: BindModel

        /// <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;
        }    
开发者ID:paulsuart,项目名称:rebelcmsxu5,代码行数:33,代码来源:HiveEntityUriModelBinder.cs

示例3: BindModel

        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;
        }
开发者ID:LByron,项目名称:BS,代码行数:39,代码来源:CheckBoxListViewModelBinder.cs

示例4: BindModel

        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;
        }
开发者ID:RadhaPatel,项目名称:aspnet-mvc-checkboxlist,代码行数:36,代码来源:CheckBoxListViewModelBinder.cs

示例5: BindModel

        /// <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;
        }
开发者ID:susamn,项目名称:generic-datatables,代码行数:59,代码来源:DatatableParamConverter.cs

示例6: BindModel

        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;
        }
开发者ID:LByron,项目名称:BS,代码行数:40,代码来源:PostViewModelBinder.cs


注:本文中的System.Web.Mvc.ModelBindingContext.GetValue方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。