数据注释仅适用于编辑

发布于 2024-11-01 01:11:38 字数 1749 浏览 0 评论 0原文

Here is my controller codes

//
        // GET: /Department/Create

        public ActionResult Create()
        {
            return View(new department());
        } 

        //
        // POST: /Department/Create

        [HttpPost]
        public ActionResult Create(department dept)
        {
            try
            {
                dbContext.AddTodepartments(dept);
                dbContext.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Department/Edit/5

        public ActionResult Edit(int id)
        {
            return View(dbContext.departments.Single(d => d.DeptID == id));
        }

        //
        // POST: /Department/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var dept = dbContext.departments.Single(d => d.DeptID == id);
            try
            {
                UpdateModel(dept);
                dbContext.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(dept);
            }
        }

Here is the partial class 

    [MetadataType(typeof(DepartmentMetaData))]
    public partial class department
    {
    }
    public class DepartmentMetaData
    {
        [Required(AllowEmptyStrings = false, ErrorMessage = "Department name required.")]
        public string DeptName { get; set; }
    }


    The required field validation is happening on the editing only. I can insert a null 'Department name' but on eding the values, it is not allowing to enter a null value.
Here is my controller codes

//
        // GET: /Department/Create

        public ActionResult Create()
        {
            return View(new department());
        } 

        //
        // POST: /Department/Create

        [HttpPost]
        public ActionResult Create(department dept)
        {
            try
            {
                dbContext.AddTodepartments(dept);
                dbContext.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Department/Edit/5

        public ActionResult Edit(int id)
        {
            return View(dbContext.departments.Single(d => d.DeptID == id));
        }

        //
        // POST: /Department/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var dept = dbContext.departments.Single(d => d.DeptID == id);
            try
            {
                UpdateModel(dept);
                dbContext.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(dept);
            }
        }

Here is the partial class 

    [MetadataType(typeof(DepartmentMetaData))]
    public partial class department
    {
    }
    public class DepartmentMetaData
    {
        [Required(AllowEmptyStrings = false, ErrorMessage = "Department name required.")]
        public string DeptName { get; set; }
    }


    The required field validation is happening on the editing only. I can insert a null 'Department name' but on eding the values, it is not allowing to enter a null value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲜肉鲜肉永远不皱 2024-11-08 01:11:38

您应该执行 ModelState.IsValid创建记录时检查。有关 ModelState 的更多信息 此处

You Should do ModelState.IsValid check when creating record. More on ModelState here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文