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


C# ModelStateDictionary.AddError方法代码示例

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


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

示例1: ValidationMessageAllowsEmptyModelName

        public void ValidationMessageAllowsEmptyModelName() {
            // Arrange
            ModelStateDictionary dictionary = new ModelStateDictionary();
            dictionary.AddError("test", "some error text");
            HtmlHelper htmlHelper = new HtmlHelper(dictionary);

            // Act 
            var html = htmlHelper.ValidationMessage("test");

            // Assert
            Assert.AreEqual(@"<span class=""field-validation-error"">some error text</span>", html.ToHtmlString());
        }
开发者ID:jesshaw,项目名称:ASP.NET-Mvc-3,代码行数:12,代码来源:ValidationHelperTest.cs

示例2: ValidationMessageAllowsEmptyModelName

        public void ValidationMessageAllowsEmptyModelName()
        {
            // Arrange
            ModelStateDictionary dictionary = new ModelStateDictionary();
            dictionary.AddError("test", "some error text");
            HtmlHelper htmlHelper = HtmlHelperFactory.Create(dictionary);

            // Act 
            var html = htmlHelper.ValidationMessage("test");

            // Assert
            Assert.Equal(@"<span class=""field-validation-error"" data-valmsg-for=""test"" data-valmsg-replace=""true"">some error text</span>", html.ToHtmlString());
        }
开发者ID:chrisortman,项目名称:aspnetwebstack,代码行数:13,代码来源:ValidationHelperTest.cs

示例3: CheckBoxUsesCustomErrorClass

        //[TestMethod]
        // Can't test as it sets a static property
        // Review: Need to redo test once we fix set once property
        public void CheckBoxUsesCustomErrorClass() {
            // Arrange
            var modelState = new ModelStateDictionary();
            modelState.AddError("foo", "error");
            HtmlHelper.ValidationInputCssClassName = "my-error-class";
            HtmlHelper helper = new HtmlHelper(modelState);

            // Act
            var html = helper.CheckBox("foo", true, new { @class = "my-class" });

            // Assert
            Assert.AreEqual(@"<input checked=""checked"" class=""my-error-class my-class"" id=""foo"" name=""foo"" type=""checkbox"" />",
                html.ToHtmlString());
        }
开发者ID:jesshaw,项目名称:ASP.NET-Mvc-3,代码行数:17,代码来源:CheckBoxTest.cs

示例4: CheckBoxWithErrorAndCustomCss

        public void CheckBoxWithErrorAndCustomCss()
        {
            // Arrange
            var modelState = new ModelStateDictionary();
            modelState.AddError("foo", "error");
            HtmlHelper helper = HtmlHelperFactory.Create(modelState);

            // Act
            var html = helper.CheckBox("foo", true, new { @class = "my-class" });

            // Assert
            Assert.Equal(@"<input checked=""checked"" class=""input-validation-error my-class"" id=""foo"" name=""foo"" type=""checkbox"" />",
                         html.ToHtmlString());
        }
开发者ID:JokerMisfits,项目名称:linux-packaging-mono,代码行数:14,代码来源:CheckBoxTest.cs

示例5: TextAreaWithCustomErrorClass

        // [Fact]
        // Cant test this in multi-threaded
        public void TextAreaWithCustomErrorClass()
        {
            // Arrange
            ModelStateDictionary modelState = new ModelStateDictionary();
            modelState.AddError("foo", "some error");
            HtmlHelper.ValidationInputCssClassName = "custom-input-validation-error";
            HtmlHelper helper = HtmlHelperFactory.Create(modelState);

            // Act
            var html = helper.TextArea("foo", String.Empty, new { @class = "my-css" });

            // Assert
            Assert.Equal(@"<textarea class=""custom-input-validation-error my-css"" cols=""20"" id=""foo"" name=""foo"" rows=""2""></textarea>",
                         html.ToHtmlString());
        }
开发者ID:marojeri,项目名称:aspnetwebstack,代码行数:17,代码来源:TextAreaHelperTest.cs

示例6: TextAreaWithErrorAndCustomCssClass

        public void TextAreaWithErrorAndCustomCssClass() {
            // Arrange
            ModelStateDictionary modelState = new ModelStateDictionary();
            modelState.AddError("foo", "some error");
            HtmlHelper helper = new HtmlHelper(modelState);

            // Act
            var html = helper.TextArea("foo", String.Empty, new { @class = "my-css" });

            // Assert
            Assert.AreEqual(@"<textarea class=""field-validation-error my-css"" cols=""20"" id=""foo"" name=""foo"" rows=""2""></textarea>",
                html.ToHtmlString());
        }
开发者ID:jesshaw,项目名称:ASP.NET-Mvc-3,代码行数:13,代码来源:TextAreaHelperTest.cs

示例7: GetModelStateWithErrors

 private static ModelStateDictionary GetModelStateWithErrors()
 {
     ModelStateDictionary modelState = new ModelStateDictionary();
     modelState.AddError("foo", "foo error <1>");
     modelState.AddError("foo", "foo error <2>");
     modelState.AddError("bar", "bar error <1>");
     modelState.AddError("bar", "bar error <2>");
     return modelState;
 }
开发者ID:chrisortman,项目名称:aspnetwebstack,代码行数:9,代码来源:ValidationHelperTest.cs

示例8: ValidationSummaryWithNoFormErrorsAndExcludedFieldErrorsReturnsNull

        public void ValidationSummaryWithNoFormErrorsAndExcludedFieldErrorsReturnsNull()
        {
            // Arrange
            ModelStateDictionary modelState = new ModelStateDictionary();
            modelState.AddError("foo", "error");
            modelState.AddError("bar", "error");

            HtmlHelper htmlHelper = HtmlHelperFactory.Create(modelState);

            // Act
            var html = htmlHelper.ValidationSummary(excludeFieldErrors: true);

            // Assert
            Assert.Null(html);
        }
开发者ID:chrisortman,项目名称:aspnetwebstack,代码行数:15,代码来源:ValidationHelperTest.cs

示例9: ListBoxWithEmptyOptionLabel

        public void ListBoxWithEmptyOptionLabel()
        {
            // Arrange
            var modelState = new ModelStateDictionary();
            modelState.AddError("foo", "some error");
            HtmlHelper helper = HtmlHelperFactory.Create(modelState);

            // Act
            var html = helper.ListBox("foo", GetSelectList(), new { @class = "my-class" });

            // Assert
            Assert.Equal(
                @"<select class=""input-validation-error my-class"" id=""foo"" name=""foo"">
<option value=""A"">Alpha</option>
<option value=""B"">Bravo</option>
<option value=""C"">Charlie</option>
</select>",
                html.ToHtmlString());
        }
开发者ID:JokerMisfits,项目名称:linux-packaging-mono,代码行数:19,代码来源:SelectHelperTest.cs

示例10: ListBoxWithErrorAndExplictAndModelState

        public void ListBoxWithErrorAndExplictAndModelState()
        {
            // Arrange
            var modelState = new ModelStateDictionary();
            modelState.SetModelValue("foo", "C");
            modelState.AddError("foo", "test");
            HtmlHelper helper = HtmlHelperFactory.Create(modelState);

            // Act
            var html = helper.ListBox("foo.bar", "Select One", GetSelectList());

            // Assert
            Assert.Equal(
                @"<select id=""foo_bar"" name=""foo.bar"">
<option value="""">Select One</option>
<option value=""A"">Alpha</option>
<option value=""B"">Bravo</option>
<option value=""C"">Charlie</option>
</select>",
                html.ToHtmlString());
        }
开发者ID:JokerMisfits,项目名称:linux-packaging-mono,代码行数:21,代码来源:SelectHelperTest.cs

示例11: ListBoxWithErrorsAndCustomClass

        public void ListBoxWithErrorsAndCustomClass() {
            // Arrange
            var modelState = new ModelStateDictionary();
            modelState.AddError("foo", "some error");
            HtmlHelper helper = new HtmlHelper(modelState);

            // Act
            var html = helper.ListBox("foo", GetSelectList(), new { @class = "my-class" });

            // Assert
            Assert.AreEqual(
                @"<select class=""field-validation-error my-class"" id=""foo"" name=""foo"">
<option value=""A"">Alpha</option>
<option value=""B"">Bravo</option>
<option value=""C"">Charlie</option>
</select>",
                html.ToHtmlString());
        }
开发者ID:adrianvallejo,项目名称:MVC3_Source,代码行数:18,代码来源:SelectHelperTest.cs

示例12: GetErrorsReturnsErrorsAddedViaAddError

        public void GetErrorsReturnsErrorsAddedViaAddError()
        {
            // Arrange
            var modelStateDictionary = new ModelStateDictionary();
            ValidationHelper validationHelper = GetValidationHelper(GetContext(), modelStateDictionary);

            // Act
            modelStateDictionary.AddError("foo", "Foo error");
            var errors = validationHelper.GetErrors("foo");

            // Assert
            Assert.Equal(new[] { "Foo error" }, errors);
        }
开发者ID:haoduotnt,项目名称:aspnetwebstack,代码行数:13,代码来源:ValidationHelperTest.cs

示例13: ListBoxWithEmptyOptionLabel

        public void ListBoxWithEmptyOptionLabel()
        {
            // Arrange
            var modelState = new ModelStateDictionary();
            modelState.AddError("foo", "some error");
            HtmlHelper helper = HtmlHelperFactory.Create(modelState);

            // Act
            var html = helper.ListBox("foo", GetSelectList(), new { @class = "my-class" });

            // Assert
            Assert.Equal(
                "<select class=\"input-validation-error my-class\" id=\"foo\" name=\"foo\">" + Environment.NewLine
              + "<option value=\"A\">Alpha</option>" + Environment.NewLine
              + "<option value=\"B\">Bravo</option>" + Environment.NewLine
              + "<option value=\"C\">Charlie</option>" + Environment.NewLine
              + "</select>",
                html.ToHtmlString());
        }
开发者ID:huyq2002,项目名称:aspnetwebstack,代码行数:19,代码来源:SelectHelperTest.cs

示例14: ListBoxWithErrorAndExplictAndModelState

        public void ListBoxWithErrorAndExplictAndModelState()
        {
            // Arrange
            var modelState = new ModelStateDictionary();
            modelState.SetModelValue("foo", "C");
            modelState.AddError("foo", "test");
            HtmlHelper helper = HtmlHelperFactory.Create(modelState);

            // Act
            var html = helper.ListBox("foo.bar", "Select One", GetSelectList());

            // Assert
            Assert.Equal(
                "<select id=\"foo_bar\" name=\"foo.bar\">" + Environment.NewLine
              + "<option value=\"\">Select One</option>" + Environment.NewLine
              + "<option value=\"A\">Alpha</option>" + Environment.NewLine
              + "<option value=\"B\">Bravo</option>" + Environment.NewLine
              + "<option value=\"C\">Charlie</option>" + Environment.NewLine
              + "</select>",
                html.ToHtmlString());
        }
开发者ID:huyq2002,项目名称:aspnetwebstack,代码行数:21,代码来源:SelectHelperTest.cs

示例15: SavePost

    public int SavePost(HttpRequestBase Request, ModelStateDictionary ms)
    {
        int postId, userId;
        string Title = Request.Unvalidated("Title"),
               Content = Request.Unvalidated("Content");

        int.TryParse(Request.Unvalidated().Form["id"], out postId);
        int.TryParse(Request.Unvalidated().Form["UserId"], out userId);

        if(string.IsNullOrWhiteSpace(Title))
            ms.AddError("Title", "Title cannot be empty!");
        if(Title.Length > 100)
            ms.AddError("Title", "Title is too long!");
        if(string.IsNullOrWhiteSpace(Content))
            ms.AddError("Content", "You have to post something!");

        if(!ms.IsValid)
            return 0;

        // tiny_mce bug?
        Content = Content.Replace("<p>&nbsp;</p>", "");

        if(postId <= 0) {
            // Insert
            string Slug = string.Empty;
            foreach(var c in Title)
                if(char.IsLetterOrDigit(c))
                    Slug += c;
                else if(char.IsWhiteSpace(c))
                    Slug += "-";

            db.Execute("INSERT INTO Posts (UserId, Published, Title, Content, Slug) VALUES (@0, @1, @2, @3, @4)", userId, DateTime.Now, Title, Content, Slug);
            postId = (int)db.GetLastInsertId();
        } else {
            // Update
            db.Execute("UPDATE Posts SET [email protected], [email protected], [email protected] WHERE [email protected]", Title, Content, DateTime.Now, postId);
        }

        return postId;
    }
开发者ID:strider-,项目名称:WebMatrix-Site,代码行数:40,代码来源:Model.cs


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