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


C# ModelStateDictionary.Add方法代码示例

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


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

示例1: ValidateSum

        public void ValidateSum(double sum, double minSum, double maxSum, ModelStateDictionary modelState, bool isInitial = false)
        {
            if (sum < minSum)
            {
                modelState.Add(isInitial ? "request.InitialSum" : "request.Sum",
                    new ModelState() {Errors = {"—умма не может быть меньше минимальной."}});
            }

            if (sum > maxSum)
            {
                modelState.Add(isInitial ? "request.InitialSum" : "request.Sum", new ModelState() { Errors = { "—умма не может быть больше максимальной." } });
                //                throw BankClientException.ThrowSumMoreThanMax();
            }
        }
开发者ID:kateEvstratenko,项目名称:Bank,代码行数:14,代码来源:ValidationService.cs

示例2: AddExcepition

        public static void AddExcepition(ModelStateDictionary ModelState, Exception ex)
        {
            if (ex is ArgumentException)
            {
                var argEx = (ArgumentException) ex;
                if (!ModelState.ContainsKey(argEx.ParamName)) ModelState.Add(argEx.ParamName, new ModelState());
                var lines = Regex.Split(ex.Message, "\r\n");
                ModelState[argEx.ParamName].Errors.Add(new ModelError(ex, lines[0]));
                return;
            }

            if (!ModelState.ContainsKey("")) ModelState.Add("", new ModelState());

            ModelState[""].Errors.Add(new ModelError(ex, ex.Message));
        }
开发者ID:tendenciaconsult,项目名称:RedivivusProd,代码行数:15,代码来源:Geral.cs

示例3: CopyTempDataToModelState

        private void CopyTempDataToModelState(ModelStateDictionary modelState,
                                              TempDataDictionary tempData)
        {
            if (!tempData.ContainsKey(TempDataKey))
            {
                return;
            }

            ModelStateDictionary fromTempData = tempData[TempDataKey]
                                                    as ModelStateDictionary;
            if (fromTempData == null)
            {
                return;
            }

            foreach (KeyValuePair<string, ModelState> pair in fromTempData)
            {
                if (modelState.ContainsKey(pair.Key))
                {
                    modelState[pair.Key].Value = pair.Value.Value;

                    foreach (ModelError error in pair.Value.Errors)
                    {
                        modelState[pair.Key].Errors.Add(error);
                    }
                }
                else
                {
                    modelState.Add(pair.Key, pair.Value);
                }
            }
        }
开发者ID:stiano,项目名称:ShopperDopper,代码行数:32,代码来源:ModelStateToTempAttribute.cs

示例4: CopyTempDataToModelState

        private void CopyTempDataToModelState(ModelStateDictionary modelState, TempDataDictionary tempData)
        {
            if (!tempData.ContainsKey(TempDataKey)) return;

            var fromTempData = tempData[TempDataKey] as ModelStateDictionary;
            if (fromTempData == null) return;

            foreach (var pair in fromTempData)
            {
                if (modelState.ContainsKey(pair.Key))
                {
                    modelState[pair.Key].Value = pair.Value.Value;

                    foreach (var error in pair.Value.Errors)
                    {
                        if (!modelState[pair.Key].Errors.Contains(error))
                            modelState[pair.Key].Errors.Add(error);
                    }
                }
                else
                {
                    modelState.Add(pair.Key, pair.Value);
                }
            }
        }
开发者ID:kenwarner,项目名称:scrilla,代码行数:25,代码来源:Attributes.cs

示例5: OnActionExecuted

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            ControllerBase controller = filterContext.Controller as ControllerBase;

              if (controller != null)
              {
            string serializedModelState = controller.TempData[Key] as string;

            if (!string.IsNullOrEmpty(serializedModelState))
            {
              IEnumerable<ModelStateWrapper> modelStateWrappers = JsonConvert.DeserializeObject<IEnumerable<ModelStateWrapper>>(serializedModelState, new JsonSerializerSettings() { Error = DeserializationErrorHandler });

              if (modelStateWrappers != null)
              {
            ModelStateDictionary modelState = new ModelStateDictionary();

            foreach (ModelStateWrapper modelStateWrapper in modelStateWrappers)
            {
              ModelState ms = new ModelState();

              ms.ValidationState = modelStateWrapper.ValidationState;
              ms.Value = new ValueProviderResult(modelStateWrapper.Value, modelStateWrapper.Value, null);
              modelState.Add(modelStateWrapper.Key, ms);
            }

            if (filterContext.Result is ViewResult)
              controller.ViewData.ModelState.Merge(modelState);

            else controller.TempData.Remove(Key);
              }
            }
              }

              base.OnActionExecuted(filterContext);
        }
开发者ID:OlegDokuka,项目名称:Platformus,代码行数:35,代码来源:ModelStateTempDataTransferAttribute.cs

示例6: Should_skip_properties_without_errors

 public void Should_skip_properties_without_errors()
 {
     var msd = new ModelStateDictionary();
     msd.Add("item.Property1", new ModelState());
     msd.AddModelError("item.Property2", "Error");
     ValidationHelpers.PropertyError pe = ValidationHelpers.MakeHierarchical(msd);
     Assert.That(pe["item"].Properties.Count, Is.EqualTo(1));
 }
开发者ID:smartinz,项目名称:randomhacking,代码行数:8,代码来源:ValidationManagerTests.cs

示例7: DoesNotAddEntries_IfNoErrorsArePresent

        public void DoesNotAddEntries_IfNoErrorsArePresent()
        {
            // Arrange
            var modelState = new ModelStateDictionary();
            modelState.Add(
                "key1",
                new ModelState() { Value = new ValueProviderResult("foo", "foo", CultureInfo.InvariantCulture) });
            modelState.Add(
                "key2",
                new ModelState() { Value = new ValueProviderResult("bar", "bar", CultureInfo.InvariantCulture) });

            // Act
            var serializableError = new SerializableError(modelState);

            // Assert
            Assert.Equal(0, serializableError.Count);
        }
开发者ID:njannink,项目名称:sonarlint-vs,代码行数:17,代码来源:SerializableErrorTests.cs

示例8: DoesNotAddEntries_IfNoErrorsArePresent

        public void DoesNotAddEntries_IfNoErrorsArePresent()
        {
            // Arrange
            var modelState = new ModelStateDictionary();
            modelState.Add(
                "key1",
                new ModelStateEntry());
            modelState.Add(
                "key2",
                new ModelStateEntry());

            // Act
            var serializableError = new SerializableError(modelState);

            // Assert
            Assert.Equal(0, serializableError.Count);
        }
开发者ID:huoxudong125,项目名称:Mvc,代码行数:17,代码来源:SerializableErrorTests.cs

示例9: SimpleModelErrorIntoJson

        public void SimpleModelErrorIntoJson()
        {
            var modelState = new ModelStateDictionary();
            modelState.Add("FirstName", new ModelState { Errors = { "First name is a required field" } });

            var jsonResult = ControllerExtensions.ToJson(modelState);

            var data = jsonResult.Data;
            Assert.That(data, Has.Property("State").Not.Empty);
            Assert.That(data, Has.Property("State").With.Some.Property("Name").EqualTo("FirstName"));
            Assert.That(data, Has.Property("State").With.Some.Property("Errors").With.Some.EqualTo("First name is a required field"));
        }
开发者ID:andy-uq,项目名称:HomeTrack,代码行数:12,代码来源:ControllerExtensionTests.cs

示例10: ModelErrorWithExceptionIntoJson

        public void ModelErrorWithExceptionIntoJson()
        {
            var modelState = new ModelStateDictionary();
            modelState.Add("FirstName", new ModelState { Errors = { new InvalidOperationException("Cannot operate") } });

            var jsonResult = ControllerExtensions.ToJson(modelState);

            var data = jsonResult.Data;
            Assert.That(data, Has.Property("State").Not.Empty);
            Assert.That(data, Has.Property("State").With.Some.Property("Name").EqualTo("FirstName"));
            Assert.That(data, Has.Property("State").With.Some.Property("Errors").With.Some.EqualTo("Cannot operate"));
        }
开发者ID:andy-uq,项目名称:HomeTrack,代码行数:12,代码来源:ControllerExtensionTests.cs

示例11: Does_Not_Overwrite_Any_Existing_ModelState_Value

        public void Does_Not_Overwrite_Any_Existing_ModelState_Value()
        {
            // Arrange
            object rawValue = new object();
            var someValue = new ModelState { Value = new ValueProviderResult(rawValue, null, null)};
            var ex = new RulesException("myProp", "myError");
            var modelState = new ModelStateDictionary();
            modelState.Add("myProp", someValue);

            // Act
            ex.AddModelStateErrors(modelState, null);

            // Assert
            Assert.Equal(1, modelState.Keys.Count());
            Assert.Same(rawValue, modelState["myProp"].Value.RawValue);
        }
开发者ID:chakrit,项目名称:xVal-mirror,代码行数:16,代码来源:RulesExceptionTests.cs

示例12: ReturnsCorrectly

        public void ReturnsCorrectly()
        {
            string key = "Key";
            string value = "Value";

            var modelState = new ModelState();
            modelState.Value = new ValueProviderResult( value, value, CultureInfo.InvariantCulture );

            var modelStateDictionary = new ModelStateDictionary();
            modelStateDictionary.Add( key, modelState );

            ModelState modelStateResult;
            var result = modelStateDictionary.GetModelStateValue( key, value.GetType(), out modelStateResult );

            Assert.AreEqual( result, value );
            Assert.AreSame( modelStateResult, modelState );
        }
开发者ID:john-t-white,项目名称:Hex,代码行数:17,代码来源:ModelStateDictionaryExtensions_GetModelStateValue.cs

示例13: WithKeyNotFoundReturnsCorrectly

        public void WithKeyNotFoundReturnsCorrectly()
        {
            string key = "Key";
            string value = "Value";

            var modelState = new ModelState();
            modelState.Value = new ValueProviderResult( value, value, CultureInfo.InvariantCulture );

            var modelStateDictionary = new ModelStateDictionary();
            modelStateDictionary.Add( key, modelState );

            ModelState modelStateResult;
            var result = modelStateDictionary.GetModelStateValue( "KeyNotFound", typeof( string ), out modelStateResult );

            Assert.IsNull( result );
            Assert.IsNull( modelStateResult );
        }
开发者ID:john-t-white,项目名称:Hex,代码行数:17,代码来源:ModelStateDictionaryExtensions_GetModelStateValue.cs

示例14: Remove_ForImplicitlyCastedToObjectExpression_RemovesModelStateKey

        public void Remove_ForImplicitlyCastedToObjectExpression_RemovesModelStateKey()
        {
            // Arrange
            var dictionary = new ModelStateDictionary();
            dictionary.Add("Child.Value", new ModelStateEntry());

            // Act
            dictionary.Remove<TestModel>(model => model.Child.Value);

            // Assert
            Assert.Empty(dictionary);
        }
开发者ID:huoxudong125,项目名称:Mvc,代码行数:12,代码来源:ModelStateDictionaryExtensionsTest.cs

示例15: ModelStatePropertyGetterWorks

        public void ModelStatePropertyGetterWorks()
        {
            // Arrange
            ApiController controller = new Mock<ApiController>().Object;

            // Act
            ModelStateDictionary expected = new ModelStateDictionary();
            expected.Add("a", new ModelState() { Value = new ValueProviders.ValueProviderResult("result", "attempted", CultureInfo.InvariantCulture) });

            controller.ModelState.Add("a", new ModelState() { Value = new ValueProviders.ValueProviderResult("result", "attempted", CultureInfo.InvariantCulture) });

            // Assert
            Assert.Equal(expected.Count, controller.ModelState.Count);
        }
开发者ID:chrissimon-au,项目名称:aspnetwebstack,代码行数:14,代码来源:ApiControllerTest.cs


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