MVC动作没有解雇

Kar*_*yan 0 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();
}
Run Code Online (Sandbox Code Playgroud)

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" />
}
Run Code Online (Sandbox Code Playgroud)

我必须在按钮点击('注册我')上保存我的模型值.提前致谢.

Rom*_*och 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();
}
Run Code Online (Sandbox Code Playgroud)

要调用某些操作,您需要提交表单.更改您buttonsubmit类型:

<input type="submit" value="Register me" /> // type="button" -> type="submit"
Run Code Online (Sandbox Code Playgroud)

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

@using (Html.BeginForm("Insert", "Employee", FormMethod.Post))
Run Code Online (Sandbox Code Playgroud)

  • 还有Html.BeginForm("**Insert**","Employee",FormMethod.Post)? (2认同)