您使用 HTML Helper 并拥有
@using(Html.BeginForm())
{
Username: <input type="text" name="username" /> <br />
Password: <input type="text" name="password" /> <br />
<input type="submit" value="Login">
<input type="submit" value="Create Account"/>
}
或使用 Url 帮助器
<form method="post" action="@Url.Action("MyAction", "MyController")" >
Html.BeginForm
有几个 (13) 覆盖,您可以在其中指定更多信息,例如,上传文件时的正常使用是使用:
@using(Html.BeginForm("myaction", "mycontroller", FormMethod.Post, new {enctype = "multipart/form-data"}))
{
< ... >
}
如果您不指定任何参数,则Html.BeginForm()
将创建一个POST
形成那个指向你当前的控制器和当前的操作。举个例子,假设你有一个名为Posts
和一个名为的动作Delete
public ActionResult Delete(int id)
{
var model = db.GetPostById(id);
return View(model);
}
[HttpPost]
public ActionResult Delete(int id)
{
var model = db.GetPostById(id);
if(model != null)
db.DeletePost(id);
return RedirectToView("Index");
}
你的 html 页面将类似于:
<h2>Are you sure you want to delete?</h2>
<p>The Post named <strong>@Model.Title</strong> will be deleted.</p>
@using(Html.BeginForm())
{
<input type="submit" class="btn btn-danger" value="Delete Post"/>
<text>or</text>
@Url.ActionLink("go to list", "Index")
}