Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.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模型绑定器和错误消息_C#_Asp.net Mvc - Fatal编程技术网

C# MVC模型绑定器和错误消息

C# MVC模型绑定器和错误消息,c#,asp.net-mvc,C#,Asp.net Mvc,验证是在Employee类中定义的,但当我发布数据时,无论文本框是否为空,Submit Action ModelState.IsValid始终为true 我的班级: public class Employee { public int EmployeeID { get; } [Required(ErrorMessage = "We need a name for this dish.")] public string EmpFirstN

验证是在Employee类中定义的,但当我发布数据时,无论文本框是否为空,Submit Action ModelState.IsValid始终为true

我的班级:

 public class Employee
    {
        public int EmployeeID { get; }

        [Required(ErrorMessage = "We need a name for this dish.")]
        public string EmpFirstName { get; set; }
        [Required]
        public string EmpLastName { get; set; }
        [Required]
        public string LoginID { get; set; }
        [Required]
        [StringLength(10)]
        public string Password { get; set; }
        [Required]
        public string MachineUserID { get; set; }
        [Required]
        public uint IqamaID { get; set; }
        [Required]
        public int Salary { get; set; }

        public int? NotMandatory { get; set; }

        public string Department { get; set; }
    }
}
 public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            HttpContextBase objContext = controllerContext.HttpContext;
            string strEmpFirstName = bindingContext.ValueProvider.GetValue("txtFirstName").AttemptedValue;//objContext.Request.Form["txtFirstName"];
            string strEmpLastName = objContext.Request.Form["txtLastName"];
            uint strIqamaID = Convert.ToUInt32(Convert.ToString(objContext.Request.Form["txtIqamaID"]));
            string strLoginID = objContext.Request.Form["txtLoginID"];
            string strMachineUserID = objContext.Request.Form["txtMachineUserID"];
            string strPassword = objContext.Request.Form["txtPassword"];
            int Salary = Convert.ToInt32(objContext.Request.Form["txtSalary"]);
            string strDeptID = objContext.Request.Form["Departments"];

            Employee objEmployee = new Employee
            { Department = strDeptID, EmpFirstName = strEmpFirstName, EmpLastName = strEmpLastName, IqamaID = strIqamaID, LoginID = strLoginID, MachineUserID = strMachineUserID, NotMandatory = 0, Password = strPassword, Salary = Salary };

            return objEmployee;

        }
 public ActionResult Submit([ModelBinder(typeof(EmployeeBinder))] Employee obj)
        {
            //

            if (ModelState.IsValid)
            {
                return View("Employee", obj);
            }
            else
            {
                return View("AddNewEmployee");
            }


        }
我的模型活页夹类:

 public class Employee
    {
        public int EmployeeID { get; }

        [Required(ErrorMessage = "We need a name for this dish.")]
        public string EmpFirstName { get; set; }
        [Required]
        public string EmpLastName { get; set; }
        [Required]
        public string LoginID { get; set; }
        [Required]
        [StringLength(10)]
        public string Password { get; set; }
        [Required]
        public string MachineUserID { get; set; }
        [Required]
        public uint IqamaID { get; set; }
        [Required]
        public int Salary { get; set; }

        public int? NotMandatory { get; set; }

        public string Department { get; set; }
    }
}
 public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            HttpContextBase objContext = controllerContext.HttpContext;
            string strEmpFirstName = bindingContext.ValueProvider.GetValue("txtFirstName").AttemptedValue;//objContext.Request.Form["txtFirstName"];
            string strEmpLastName = objContext.Request.Form["txtLastName"];
            uint strIqamaID = Convert.ToUInt32(Convert.ToString(objContext.Request.Form["txtIqamaID"]));
            string strLoginID = objContext.Request.Form["txtLoginID"];
            string strMachineUserID = objContext.Request.Form["txtMachineUserID"];
            string strPassword = objContext.Request.Form["txtPassword"];
            int Salary = Convert.ToInt32(objContext.Request.Form["txtSalary"]);
            string strDeptID = objContext.Request.Form["Departments"];

            Employee objEmployee = new Employee
            { Department = strDeptID, EmpFirstName = strEmpFirstName, EmpLastName = strEmpLastName, IqamaID = strIqamaID, LoginID = strLoginID, MachineUserID = strMachineUserID, NotMandatory = 0, Password = strPassword, Salary = Salary };

            return objEmployee;

        }
 public ActionResult Submit([ModelBinder(typeof(EmployeeBinder))] Employee obj)
        {
            //

            if (ModelState.IsValid)
            {
                return View("Employee", obj);
            }
            else
            {
                return View("AddNewEmployee");
            }


        }
我的控制器操作:

 public class Employee
    {
        public int EmployeeID { get; }

        [Required(ErrorMessage = "We need a name for this dish.")]
        public string EmpFirstName { get; set; }
        [Required]
        public string EmpLastName { get; set; }
        [Required]
        public string LoginID { get; set; }
        [Required]
        [StringLength(10)]
        public string Password { get; set; }
        [Required]
        public string MachineUserID { get; set; }
        [Required]
        public uint IqamaID { get; set; }
        [Required]
        public int Salary { get; set; }

        public int? NotMandatory { get; set; }

        public string Department { get; set; }
    }
}
 public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            HttpContextBase objContext = controllerContext.HttpContext;
            string strEmpFirstName = bindingContext.ValueProvider.GetValue("txtFirstName").AttemptedValue;//objContext.Request.Form["txtFirstName"];
            string strEmpLastName = objContext.Request.Form["txtLastName"];
            uint strIqamaID = Convert.ToUInt32(Convert.ToString(objContext.Request.Form["txtIqamaID"]));
            string strLoginID = objContext.Request.Form["txtLoginID"];
            string strMachineUserID = objContext.Request.Form["txtMachineUserID"];
            string strPassword = objContext.Request.Form["txtPassword"];
            int Salary = Convert.ToInt32(objContext.Request.Form["txtSalary"]);
            string strDeptID = objContext.Request.Form["Departments"];

            Employee objEmployee = new Employee
            { Department = strDeptID, EmpFirstName = strEmpFirstName, EmpLastName = strEmpLastName, IqamaID = strIqamaID, LoginID = strLoginID, MachineUserID = strMachineUserID, NotMandatory = 0, Password = strPassword, Salary = Salary };

            return objEmployee;

        }
 public ActionResult Submit([ModelBinder(typeof(EmployeeBinder))] Employee obj)
        {
            //

            if (ModelState.IsValid)
            {
                return View("Employee", obj);
            }
            else
            {
                return View("AddNewEmployee");
            }


        }

你为什么不能让它像这样自动绑定

public ActionResult Submit(Employee obj)
    {
        //

        if (ModelState.IsValid)
        {
            return View("Employee", obj);
        }
        else
        {
            return View("AddNewEmployee");
        }


    }