单击按钮时未执行操作方法

时间:2016-04-29 14:13:28

标签: asp.net-mvc

我制作了一个简单的MVC程序。我创建了3个按钮来插入,更新或删除员工。但是当我点击按钮时,动作方法没有被执行&为什么相同的代码在我朋友的系统中起作用,但在我的系统中不起作用。我们都使用相同的MVC 4.这里的代码

控制器

public ActionResult Edit_Update_Delete_Insert_EMP()
    {

        EmployeeModel em = new EmployeeModel();
        ActionResult ar = View(em);
        return ar;
    }

    [HttpPost]
    public ActionResult Edit_Update_Delete_Insert_EMP(EmployeeModel em, string Employee)
    {
        if (Employee == "Insert")
        { 
        }
        if (Employee == "Update")
        { 
        }
        if (Employee == "Delete")
        { 
        }
        return null;
    }

查看

    @model Deepak_MVC_1.Models.EmployeeModel
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Edit_Update_Delete_Insert_EMP</title>
</head>
<body>
    <div>
        @using( Html.BeginForm())
        {
            <table>
                <tr>
                    <td>@Html.LabelFor(eid=>eid.E_ID)</td>
                    <td>@Html.TextBoxFor(eid=>eid.E_ID)</td>
                </tr>
                <tr>
                    <td>@Html.LabelFor(fname=>fname.F_Name)</td>
                    <td>@Html.TextBoxFor(fname=>fname.F_Name)</td>
                </tr>
                <tr>
                    <td>@Html.LabelFor(lname=>lname.L_Name)</td>
                    <td>@Html.TextBoxFor(lname=>lname.L_Name)</td>
                </tr>
                <tr>
                    <td>@Html.LabelFor(age=>age.Age)</td>
                    <td>@Html.TextBoxFor(age=>age.Age)</td>
                </tr>
                <tr>
                    <td>@Html.LabelFor(salary=>salary.Salary)</td>
                    <td>@Html.TextBoxFor(salary=>salary.Salary)</td>
                </tr>
                <tr>
                    <td>@Html.LabelFor(dept=>dept.Dept)</td>
                    <td>@Html.TextBoxFor(dept=>dept.Dept)</td>
                </tr>
                <tr>
                    <td>@Html.LabelFor(doj=>doj.Doj)</td>
                    <td>@Html.TextBoxFor(doj=>doj.Doj)</td>
                </tr>
            </table>
        }   
            <button type="submit"name="Employee" value="Insert">Insert Employee</button>
            <button type="submit"name="Employee" value="Update">Update Employee</button>
            <button type="submit"name="Employee" value="Delete">Delete Employee</button>
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

按钮位于BeginForm() { }块之外,因此未提交Employee数据值(由单击的按钮确定)。