C# ASP.NET MVC 表单发布
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/317225/
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
ASP.NET MVC Form Post
提问by Tablet
<form action="/Villa/Add" method="post">
<table>
<tr>
<td>
Name:
</td>
<td>
<%= Html.TextBox("name") %>
<%= Html.ValidationMessage("Name") %>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" value="Add" />
</td>
</tr>
</table>
</form>
My form is above, how do I retrieve the values in my controller?
我的表单在上面,如何检索控制器中的值?
Thanks a lot in advance! Hard to find the right material because of different Previews of MVC being released and being different.
非常感谢!很难找到合适的材料,因为 MVC 的不同预览版本正在发布并且不同。
采纳答案by tvanfosson
This works for ASP.Net MVC Beta.
这适用于 ASP.Net MVC Beta。
public ActionResult Add( string name ) {
....
}
or
public ActionResult Add( FormCollection form ) {
string name = form["Name"];
}
or
public ActionResult Add( [Bind(Prefix="")]Villa villa ) {
villa.Name ...
}
回答by Mariusz
It belongs to your url routes, you defined.
它属于您定义的 url 路由。
In your case the form ist looking for an controller named "Villa" and the action inside of it named "Add".
在您的情况下,表单正在寻找名为“Villa”的控制器及其内部名为“Add”的操作。
Maybe you should read ScottGu's blog post: http://weblogs.asp.net/scottgu/archive/2008/09/02/asp-net-mvc-preview-5-and-form-posting-scenarios.aspx
也许您应该阅读 ScottGu 的博客文章:http: //weblogs.asp.net/scottgu/archive/2008/09/02/asp-net-mvc-preview-5-and-form-posting-scenarios.aspx
回答by Jeff Sheldon
Have you tried something like this? Pseudocode...
你有没有尝试过这样的事情?伪代码...
public class VillaController : Controller
{
public ActionResult Add(string name)
{
// Code...
}
}