如何将 HTML5 表单操作链接到 ASP.NET MVC 4 中的控制器 ActionResult 方法 [英] How to link HTML5 form action to Controller ActionResult method in ASP.NET MVC 4
问题描述
我有一个基本表单,我想通过调用视图关联的 Controller
类中的 ActionResult
方法来处理表单内的按钮.这是表单的以下 HTML5 代码:
欢迎光临
<div><h3>登录</h3><form method="post" action= <!-- 这里是什么-->>用户名:<input type="text" name="username"/><br/>密码:<input type="text" name="password"/><br/><input type="submit" value="登录"><input type="submit" value="创建账户"/></表单>
<!-- 更多代码...-->
对应的Controller
代码如下:
[HttpPost]公共 ActionResult MyAction(字符串输入,FormCollection 集合){开关(输入){案例登录"://做一些事情...休息;案例创建帐户"//做一些其他的事情...休息;}返回视图();}
您使用 HTML Helper 并具有
@using(Html.BeginForm()){用户名:<input type="text" name="username"/><br/>密码:<input type="text" name="password"/><br/><input type="submit" value="登录"><input type="submit" value="创建账户"/>}
或使用 Url 助手
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 模型 = db.GetPostById(id);返回视图(模型);}[HttpPost]公共操作结果删除(int id){var 模型 = db.GetPostById(id);如果(模型!= null)db.DeletePost(id);return RedirectToView("索引");}
而您的 html 页面将类似于:
您确定要删除吗?</h2><p>名为<strong>@Model.Title</strong>的帖子</p>@using(Html.BeginForm()){<input type="submit" class="btn btn-danger" value="删除帖子"/><text>或</text>@Url.ActionLink("转到列表", "索引")}
I have a basic form for which I want to handle buttons inside the form by calling the ActionResult
method in the View's associated Controller
class. Here is the following HTML5 code for the form:
<h2>Welcome</h2>
<div>
<h3>Login</h3>
<form method="post" action= <!-- what goes here --> >
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"/>
</form>
</div>
<!-- more code ... -->
The corresponding Controller
code is the following:
[HttpPost]
public ActionResult MyAction(string input, FormCollection collection)
{
switch (input)
{
case "Login":
// do some stuff...
break;
case "Create Account"
// do some other stuff...
break;
}
return View();
}
you make the use of the HTML Helper and have
@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"/>
}
or use the Url helper
<form method="post" action="@Url.Action("MyAction", "MyController")" >
Html.BeginForm
has several (13) overrides where you can specify more information, for example, a normal use when uploading files is using:
@using(Html.BeginForm("myaction", "mycontroller", FormMethod.Post, new {enctype = "multipart/form-data"}))
{
< ... >
}
If you don't specify any arguments, the Html.BeginForm()
will create a POST
form that points to your current controller and current action. As an example, let's say you have a controller called Posts
and an action called 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");
}
and your html page would be something like:
<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")
}
这篇关于如何将 HTML5 表单操作链接到 ASP.NET MVC 4 中的控制器 ActionResult 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!