MVC ModelState.IsValid不起作用

时间:2016-04-09 12:18:14

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-5

我使用下面的代码来验证我的模型,但是当我点击“保存员工”时#39; CreateEmployee视图上的按钮,页面返回CreateEmployee视图,没有验证消息。

模型

public class Employee
{
   [Key]
   public int EmployeeId { get; set; }
   [Required(ErrorMessage="Enter First Name")]
   public string FirstName { get; set; }
   [Required(ErrorMessage="Enter Last Name")]
   [StringLength(6,ErrorMessage="Last Name should not contain more than 6 characters")]
   public string LastName { get; set; }
   public int Salary { get; set; }
}

查看

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    @*<meta name="viewport" content="width=device-width" />*@
    <title>CreateEmployee</title>
    <script type="text/javascript">
        function ResetForm() {
            document.getElementById("txtFirstName").value = "";
            document.getElementById("txtLastName").value = "";
            document.getElementById("txtSalary").value = "";
        }
    </script>
</head>
<body>
    <div>
        <form action="SaveEmployee" method="post">
            @Html.ValidationSummary()
            <table>
                <tr>
                    <td>First Name:</td>
                    <td>
                        <input type="text" id="txtFirstName" name="FirstName" value="" /></td>
                </tr>
                <tr>
                    <td colspan="2" align="right">@Html.ValidationMessage("FirstName")</td>
                </tr>
                <tr>
                    <td>Last Name:</td>
                    <td>
                        <input type="text" id="txtLastName" name="LastName" value="" /></td>
                </tr>
                <tr>
                    <td colspan="2" align="right">@Html.ValidationMessage("LastName")
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="submit" value="SaveEmployee" />
                    </td>
                </tr>
            </table>
        </form>
    </div>
</body>
</html>  

控制器

public ActionResult SaveEmployee(Employee e,string BtnSubmit)
{
switch (BtnSubmit)
{
case "Save Employee":
if (ModelState.IsValid)
{
EmployeeBuisnessLayer objEBL = new EmployeeBuisnessLayer();
objEBL.SaveEmployee(e);
return RedirectToAction("Index");
}
else
{
return RedirectToAction("CreateEmployee");
}
case "Cancel":
return RedirectToAction("Index");
}
return new EmptyResult();
}

1 个答案:

答案 0 :(得分:1)

不要重定向。你将失去模型状态。您需要让方法运行完成并将模型传递回视图。

<强>控制器

[HttpPost]
public ActionResult CreateEmployee(Employee e, string BtnSubmit) {
    switch (BtnSubmit) {
        case "SaveEmployee":
            if (ModelState.IsValid) {
                EmployeeBuisnessLayer objEBL = new EmployeeBuisnessLayer();
                objEBL.SaveEmployee(e);
                return RedirectToAction("Index");
            }
        case "Cancel":
            return RedirectToAction("Index");
        default:
            return new EmptyResult();
    }

    return View(e);
}

您的表单还需要发布到CreateEmployee

查看

<form action="CreateEmployee" method="post">