简体   繁体   English

ASP.NET MVC Form Post

[英]ASP.NET MVC Form Post

   <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的不同预览被发布和不同。

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 ...
 }

Have you tried something like this? 你尝试过这样的事吗? Pseudocode... 伪...

public class VillaController : Controller 
{
      public ActionResult Add(string name)
      {
          // Code...
      }
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM