本文整理汇总了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();
}
}
示例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));
}
示例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);
}
}
}
示例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);
}
}
}
示例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);
}
示例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));
}
示例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);
}
示例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);
}
示例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"));
}
示例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"));
}
示例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);
}
示例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 );
}
示例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 );
}
示例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);
}
示例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);
}