Html 如何将 HTML5 表单操作链接到 ASP.NET MVC 4 中的控制器 ActionResult 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15190929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to link HTML5 form action to Controller ActionResult method in ASP.NET MVC 4
提问by dtg
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:
我有一个基本表单,我想通过调用ActionResult
视图关联Controller
类中的方法来处理表单内的按钮。这是表单的以下 HTML5 代码:
<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:
对应的Controller
代码如下:
[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();
}
回答by balexandre
you make the use of the HTML Helper and have
您使用 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"/>
}
or use the Url helper
或使用 Url 助手
<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:
Html.BeginForm
有几个 (13) 覆盖,您可以在其中指定更多信息,例如,上传文件时的正常使用正在使用:
@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
如果您不指定任何参数,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");
}
and your html page would be something like:
并且您的 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")
}
回答by jade290
Here I'm basically wrapping a button in a link. The advantage is that you can post to different action methods in the same form.
在这里,我基本上是在链接中包装一个按钮。优点是您可以在同一表单中发布到不同的操作方法。
<a href="Controller/ActionMethod">
<input type="button" value="Click Me" />
</a>
Adding parameters:
添加参数:
<a href="Controller/ActionMethod?userName=ted">
<input type="button" value="Click Me" />
</a>
Adding parameters from a non-enumerated Model:
从非枚举模型添加参数:
<a href="Controller/[email protected]">
<input type="button" value="Click Me" />
</a>
You can do the same for an enumerated Model too. You would just have to reference a single entity first. Happy Coding!
您也可以对枚举模型执行相同操作。您只需要先引用一个实体。快乐编码!