繁体   English   中英

ASP.NET MVC:存在字段,但验证错误为“必填字段”

[英]ASP.NET MVC: field is present but validation error says “Field is required”

摘要:

当我想更新数据库中的Model实例时,它会导致验证错误Client字段Field is required字段(保存之前,Altough字段已填写,并且在我跟踪程序时,该字段不为空)。

细节:

我有一个下面列出的模型Project

namespace ProjectManager.Models
{
    public class Project
    {
        [Key]
        public int Id { get; set; }

        [Required]
        [Display(Name = "نام پروژه")]
        [StringLength(100, MinimumLength = 5, ErrorMessage = "{0} باید حداقل {2} و حداکثر {1} کاراکتری باشد")]
        public string Name { get; set; }

        [Display(Name = "کارفرما")]
        [Required]
        public virtual ApplicationUser Client { get; set; }

        [ForeignKey("Client")]
        public string ClientId{ get; set; }


        [Display(Name = "مدیر پروژه")]
        [Required]
        public virtual ApplicationUser ProjectManager { get; set; }

        [ForeignKey("ProjectManager")]
        public string ProjectManagerId{ get; set; }

        [Range(0,100)]
        [Display(Name = "پیشرفت پروژه")]
        [Required]
        public int Progress { get; set; }

        [DataType(DataType.Date)]
        [Display(Name = "تاریخ ثبت پروژه")]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime CreateDate { get; set; }

        //[Required]
        [Column("Disabled")]
        [Display(Name = "غیرفعال")]
        public bool Disabled{ get; set; }

        //[Required]
        [Column("Status")]
        [Display(Name = "وضعیت")]
        public string Status{ get; set; }

        //-------------------- Payment Requests of Projects
        public virtual ICollection<PaymentRequest> PaymentRequests { get; set; }
        public virtual ICollection<Contract> Contracts { get; set; }

    }
}

当我想更改项目进度时,请使用以下控制器:

    public ActionResult UpdateProgress()
    {
        ApplicationUser projectManager = db.Users.Find(User.Identity.GetUserId());
        Project project = Utils.getProjectManagerProject(projectManager, db);
        EditProgressProjectViewModel model = new EditProgressProjectViewModel
        {
            Progress = project.Progress
        };
        return View(model);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult UpdateProgress(EditProgressProjectViewModel projectModel)
    {
        if (projectModel == null)
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        if (ModelState.IsValid)
        {
            ApplicationUser projectManager = db.Users.Find(User.Identity.GetUserId());
            Project project = Utils.getProjectManagerProject(projectManager, db);
            project.Progress = projectModel.Progress;
            try
            {
                db.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                Utils.addValidationErrorsToModelState(ModelState, e);
                return View(projectModel);
            }
            return RedirectToAction("Index");
        }
        return View(projectModel);
    }
}

但是尝试运行db.saveChanges()会导致验证错误,提示“需要Field Client”,但project.client不为空,并且已填充有已经从数据库中获取的值。

问题是[Required]是在错误的地方定义的,这意味着要使用以下声明:

    [Display(Name = "کارفرما")]
    [Required]
    public virtual ApplicationUser Client { get; set; }

    [ForeignKey("Client")]
    public string ClientId{ get; set; }

必须使用:

    [Display(Name = "کارفرما")]
    public virtual ApplicationUser Client { get; set; }

    [ForeignKey("Client")]
    [Required] //**** Right Place for required annotation****
    public string ClientId{ get; set; }

[Required]应放在对ForeignKey ID不ForeignKey对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM