Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# mvc 3验证注释错误消息不会显示在字段窗体旁边_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 3 - Fatal编程技术网

C# mvc 3验证注释错误消息不会显示在字段窗体旁边

C# mvc 3验证注释错误消息不会显示在字段窗体旁边,c#,asp.net,asp.net-mvc,asp.net-mvc-3,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 3,我是mvc新手,我有一个消息传递类 [Table("employeeTable")] public class Messsaging : IValidatableObject { public virtual int id { set; get; } [Required] public virtual String name { set; get; } [Required] public virtual String country { set; get;

我是mvc新手,我有一个消息传递类

[Table("employeeTable")]
public class Messsaging : IValidatableObject
{
    public virtual int id { set; get; }
    [Required]
    public virtual String name { set; get; }
    [Required]
    public virtual String country { set; get; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (name.Equals(null))
        {
            yield return new ValidationResult("this field can'not be empty");
        }
    }
}
当我提交表单时,我会在项目中收到一条验证错误异常消息,而不是在我的创建表单上显示错误消息!!任何想法,请提前向大家表示感谢。

您需要在保存之前检查模型状态,如果无效,请返回视图

[HttpPost]
public ActionResult Create(Messsaging form)
{
  if (!ModelState.IsValid)
  {
    return View(form); // returns the view with errors
  }
  // It OK so save
}

您忘记了在ValidationResult构造函数中设置字段名,并使用string.IsNullOrWhiteSpace检查name属性的值

[Table("employeeTable")]
public class Messsaging : IValidatableObject
{
    public virtual int id { set; get; }
    [Required]
    public virtual String name { set; get; }
    [Required]
    public virtual String country { set; get; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (string.IsNullOrWhiteSpace(name))
        {
            yield return new ValidationResult("this field can'not be empty", new[] { "name" });
        }
    }
}

您好,Peter,谢谢您的回答,问题是,即使我删除了自定义验证方法并坚持使用内置方法一次,也不会显示任何验证消息,相反,我会抛出一个错误并指向saveChanges。创建操作方法中的行打开InnerException-s,您将看到错误是什么,但是Stephen指出您至少应该使用ModelState进行错误检查。非常感谢Stephen!!现在可以了,我的指导老师在动作方法中没有上述if条件的情况下编写了代码,但他使用的是mvc3,我使用的是mvc4,你认为这是导致问题的原因吗?非常感谢SirAFAIK,MVC 3和MVC 4在这方面没有区别
[Table("employeeTable")]
public class Messsaging : IValidatableObject
{
    public virtual int id { set; get; }
    [Required]
    public virtual String name { set; get; }
    [Required]
    public virtual String country { set; get; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (string.IsNullOrWhiteSpace(name))
        {
            yield return new ValidationResult("this field can'not be empty", new[] { "name" });
        }
    }
}