标签: data-annotations

WebGrid和EF4属性

有没有办法使用DataAnnotations属性与MVC3 WebGrid扩展?

我使用了一些EF4 DataAnnotations属性,如[ScaffoldColumn(true)]和[Display(Description ="Name",Prompt ="Enter name",ShortName ="Name")].但它对WebGrid没有任何影响.

那么如果可能的话,如何将我的DataAnnotations属性与WebGrid一起使用呢?


更新好,Darin Dimitrov对我的问题做了很好的回答.而且,它是+1.但是对于工作来说,MVCContrib比WebGrid要好得多.这是我的选择.

asp.net-mvc entity-framework data-annotations asp.net-mvc-3

4
推荐指数
1
解决办法
1498
查看次数

ASP.Net MVC:你可以在AJAX/jQuery调用中使用数据注释/验证吗?

你可以在AJAX/jQuery调用中使用Data Annotations/Validation吗?如果是这样,请提供示例或帖子,其中显示了一个示例.

基本上我已经看到了一个如何使用数据注释的例子,但它有一个完整的帖子.有没有办法处理AJAX/jQuery调用?不知道如何做到这一点因为我不确定如何在客户端构造Model对象.(我认为这是你必须要做的.)

有人告诉我这可以做到,但我只是不明白它是怎么回事.

谢谢你的帮助.

ajax asp.net-mvc data-annotations

4
推荐指数
1
解决办法
7825
查看次数

如何正确制作byte []字段必填字段?

我需要byte[]在我的模型中验证 a ,Required但是每当我使用Data Annotation [Required]它时,它都不会做任何事情。即使我选择了一个文件,它也会输出错误消息。

细节:

模型:

Public class MyClass
{
   [Key]
   public int ID {get; set;}

   [Required]
   public string Name {get; set;}

   public byte[] Image {get; set;}

   [Required]
   public byte[] Template {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

看法:

<div class="editor-label">
   <%:Html.LabelFor(model => model.Image) %>
</div>
<div class="editor-field">
   <input type="file" id="file1" name="files" />
</div>
<div class="editor-label">
   <%:Html.Label("Template") %>
</div>
<div class="editor-field"> 
   <input type="file" id="file2" name="files"/>
</div>
<p>
   <input type="submit" value="Create" />
</p>
Run Code Online (Sandbox Code Playgroud)

我环顾了帖子并注意到人们使用自定义验证,但由于某种原因,当我尝试使用相同的 it 错误并缺少 ID 时,他们已将其用作 …

.net c# asp.net-mvc entity-framework data-annotations

4
推荐指数
1
解决办法
3080
查看次数

自定义名称到枚举值

我有一个通过API传递的值的枚举.

这些名称很好,但有一个我想用数据注释改变,但你怎么做?

我的枚举看起来像:

public enum TopicType
{
    All = 0,
    Message=1,
    CalendarEvent=2,
    Upload=4, 
    ToDo=8,
    ToDoList=16,
    Document=32
}
Run Code Online (Sandbox Code Playgroud)

我想在编码时将"ToDo"更改为"ToDoItem",但由于我的枚举对象的序列化,我无法更改枚举中的值,因此我将不得不使用数据注释,任何建议?

c# enums data-annotations

4
推荐指数
2
解决办法
7617
查看次数

MVC3通过ModelState添加和显示消息

我有一个场景,我需要在Edit [GET]请求中显示来自DB的错误消息.

我知道如果请求类型是[POST],这可以完成,但我们如何在[GET]请求中执行此操作.

相同代码:

    [HttpGet]
    public ActionResult Edit(Int64 ID)
      {
         tblSample1 model = GetData(ID);
         ViewData.ModelState.AddModelError(model.Username, "Invalid Username provided.");
         return View("~/Views/Sample1/_Edit.cshtml", model);
      }

[HttpPost] public ActionResult Edit(tblSample1 model) { if (ModelState.IsValid) { ...... ...... } }
Run Code Online (Sandbox Code Playgroud)

c# modelstate data-annotations asp.net-mvc-3

4
推荐指数
1
解决办法
6522
查看次数

使用 DataAnnotations 与正则表达式不匹配

是否可以使用DataAnnotation和正则表达式来过滤文本框中的条目?即当字符串中出现“apt”一词时触发无效响应?

c# regex asp.net-mvc entity-framework data-annotations

4
推荐指数
1
解决办法
4154
查看次数

Asp.net Web Api嵌套模型验证

我在asp.net web api的模型绑定和验证(通过数据注释)中遇到了一些问题.

好像我有一个属性的模型,如

Dictionary<string, childObject> obj { get; set; }
Run Code Online (Sandbox Code Playgroud)

childObject的验证似乎没有触发.数据从json与Json.Net序列化器绑定.

是否有一些解决方法或修复此问题?或者我误解了与此相关的其他内容?


我不禁想知道为什么这不会导致错误:

public class Child
{        
    [Required]
    [StringLength(10)]
    public string name;
    [Required]
    [StringLength(10)]
    public string desc;     
}

//elsewhere
Child foo = new Child();
foo.name = "hellowrodlasdasdaosdkasodasasdasdasd";

List<ValidationResult> results = new List<ValidationResult>();
Validator.TryValidateObject(foo, new ValidationContext(foo), results, true);
// results.length == 0 here.
Run Code Online (Sandbox Code Playgroud)

天啊.我忘了声明属性而不是字段.

c# data-annotations asp.net-web-api

4
推荐指数
1
解决办法
2419
查看次数

无法在 .net core 1.0 中使用数据注释

就像早期测试版 (dnx) 中的这个问题一样,我正在尝试将代码移植到 .net core 1.0 rtm,并且代码在以下包含 ValidationAttribute 类型和其他内容的单元上回复:

using System.ComponentModel.DataAnnotations;

namespace Hl7.Fhir.Introspection
{
    [AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
    public sealed class FhirElementAttribute : ValidationAttribute
    ...
Run Code Online (Sandbox Code Playgroud)

如何将此类代码移植到 dotnet core 1.0 rtm?

c# data-annotations visual-studio-2015 .net-core

4
推荐指数
1
解决办法
1277
查看次数

无论DataAnnotations属性如何,ModelState.IsValid始终为true

我在Visual Studio 2015中使用了新的MVC6框架,突然我的所有Data Annotations都停止了工作.所有这些,没有我改变代码.

public sealed class RegisterUser
{
    [Required(ErrorMessage = "required")]
    [RegularExpression(@"^((.|\n)*)$", ErrorMessage = "regex")]
    [StringLength(32, MinimumLength = 3, ErrorMessage = "length")]
    public string Name { get; set; }

    ...
}
Run Code Online (Sandbox Code Playgroud)

[Route(Address + "/membership")]
public class MembershipController : Controller
{
    // POST [address]/membership/register
    [AllowAnonymous]
    [HttpPost("Register")]
    public IActionResult Register([FromBody]RegisterUser model)
    {
        // Validate the input model.
        if (model == null)
            return ...

        if (!ModelState.IsValid)
            return ... 

        // Always get HERE 
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么我在地球上传递"ModelState.IsValid"测试(它总是评估为真)?

例如,我传递Name ="x",它仍然评估为true.好像注释不在那里.

它与使用MvcCore有关吗?

data-annotations asp.net-core-mvc

4
推荐指数
1
解决办法
1975
查看次数

自定义模型验证器,用于ASP.NET Core Web API中的整数值

我已经开发了一个自定义验证器Attribute类,用于检查模型类中的Integer值。但问题是此类无法正常工作。我已经调试了我的代码,但是在调试代码期间没有遇到断点。这是我的代码:

public class ValidateIntegerValueAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value != null)
            {
                int output;

                var isInteger = int.TryParse(value.ToString(), out output);

                if (!isInteger)
                {
                    return new ValidationResult("Must be a Integer number");
                }
            }

            return ValidationResult.Success;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我还有一个Filter类,用于在应用程序请求管道中全局进行模型验证。这是我的代码:

public class MyModelValidatorFilter: IActionFilter
{   
    public void OnActionExecuting(ActionExecutingContext context)
    {
        if (context.ModelState.IsValid)
            return;

        var errors = new Dictionary<string, string[]>();

        foreach (var err in actionContext.ModelState)
        {
            var itemErrors = new List<string>();

            foreach (var error in err.Value.Errors){ …
Run Code Online (Sandbox Code Playgroud)

model-validation data-annotations asp.net-core-2.0

4
推荐指数
1
解决办法
2303
查看次数