MVC动作没有解雇

时间:2017-02-21 07:24:42

标签: c# asp.net asp.net-mvc

控制器:

public ActionResult Insert()
{
    return View();
}
public ActionResult Insert(Employee emp)
{
    Employee emp1 = new Employee();
    emp1.insert(emp);
    return View();
}

CSHTML

@using (Html.BeginForm("Employee", "Insert", FormMethod.Post))
{
    @Html.TextBoxFor(model => model.name)
    @Html.TextBoxFor(model => model.Email)
    @Html.TextBoxFor(model => model.mob)

    <input type="button" value="Register me" />
}

我必须在点击按钮时保存我的模型值(&#39;注册我&#39;)。提前致谢。

2 个答案:

答案 0 :(得分:7)

尝试在控制器中设置属性:

[HttpGet] // here ([HttpGet] is default so here you can delete this attribute)
public ActionResult Insert()
{
    return View();
}

[HttpPost]  // here
public ActionResult Insert(Employee emp)
{
    Employee emp1 = new Employee();
    emp1.insert(emp);
    return View();
}

要调用某些操作,您需要提交表单。将您的button更改为submit类型:

<input type="submit" value="Register me" /> // type="button" -> type="submit"

此外,在BeginForm中,您应首先指定操作名称,然后指定控制器名称:

@using (Html.BeginForm("Insert", "Employee", FormMethod.Post))

答案 1 :(得分:3)

因为您尚未在HTTP POST

上声明INSERT action
   [HttpPost]
   public ActionResult Insert(Employee emp)
    {
        Employee emp1 = new Employee();
        emp1.insert(emp);
        return View();
    }

当您使用Beginform并且FormMethodPost时,相关的操作需要具有相同类型的HTTP,顺便说一下,如果您拥有{并不重要{第一个[HttpGet] {1}}因为在MVC中,任何未声明任何类型的HTTP请求/响应的ActionResult都称为ActionResult

同样在您的BeginForm()中:

[HttpGet]
相关问题