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


C# ModelStateDictionary.First方法代码示例

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


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

示例1: BindRouteProperties

      /// <summary>
      /// Binds controller properties decorated with <see cref="FromRouteAttribute"/>
      /// using the current route values.
      /// </summary>
      /// <param name="controller">The controller to bind.</param>
      /// <remarks>You can call this method from <see cref="ControllerBase.Initialize"/>.</remarks>
      public static void BindRouteProperties(this ControllerBase controller) {

         if (controller == null) throw new ArgumentNullException("controller");

         var controllerData = controllerDataCache
            .GetOrAdd(controller.GetType(), (type) => new ControllerData(type));

         if (controllerData.Properties.Length == 0) {
            return;
         }

         var modelState = new ModelStateDictionary();

         var bindingContext = new ModelBindingContext {
            FallbackToEmptyPrefix = true,
            ModelState = modelState
         };

         for (int i = 0; i < controllerData.Properties.Length; i++) {

            var propertyData = controllerData.Properties[i];

            propertyData.BindProperty(controller.ControllerContext, bindingContext);

            if (!modelState.IsValid) {
               break;
            }
         }

         if (!modelState.IsValid) {

            ModelError error = modelState.First(m => m.Value.Errors.Count > 0).Value.Errors.First(); 
            
            int statusCode = 404;
            string message = "Not Found";

            if (error.Exception != null) {
               throw new HttpException(statusCode, message, error.Exception);
            }

            throw new HttpException(statusCode, message);
         }
      }
开发者ID:brettveenstra,项目名称:MvcCodeRouting,代码行数:49,代码来源:MvcExtensions.cs

示例2: GetTheFirstPropertyWithAnError

 private static string GetTheFirstPropertyWithAnError(ModelStateDictionary modelState, string prefixValue)
 {
     return modelState.First(x => x.Value.Errors.Count() > 0 && x.Key.StartsWith(prefixValue)).Key;
 }
开发者ID:burkhartt,项目名称:Bennington,代码行数:4,代码来源:ValidationSummaryHelpers.cs


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