zoukankan      html  css  js  c++  java
  • MVC 表单提交

    用户提交表单

    写法一(推荐)

    一,不带参数

     1 <body>  
     2     <!--一下写法生成:<form action="/Home/Index" method="post"> BeginForm里不带参数,就表示请求从哪里来,表单就提交到哪里去。  
     3         因为当前视图是Home控制器下的Index视图。所以,当请求这个Index视图的时候,form的action的提交的地址就是/Home/Index  
     4         -->  
     5     @using (Html.BeginForm("Add","Home",new{ id=200}))  
     6     {  
     7         <input type="text" name="userName" />  
     8     }  
     9   
    10 </body>

    二,带参数

    1 <body>  
    2     <!--一下写法生成:<form action="/Home/Add/200?pric=25" method="get">  -->  
    3     @using (Html.BeginForm("Add","Home",new{ id=200,pric=25},FormMethod.Get))  
    4     {  
    5         <input type="text" name="userName" />  
    6     }  
    7   
    8 </body> 

    三,程序员自己指定一个路由,来生成一个action的URL。使用Html.BeginRouteForm(...)

    不带参数:

    1 <body>  
    2     <!--由程序员指定一个路由规则,来生成一个action的URL  -->  
    3     <!--以下代码最后生成这样:<form action="/Index/Home" method="post">  注意:按照Default2这个路由规则来生成的,所有Index在前面-->  
    4     @using (Html.BeginRouteForm("Default2"))  
    5     {  
    6         <input type="text" name="userName" />  
    7     }  
    8   
    9 </body> 

    带参数:

    1 <body>  
    2     <!--由程序员指定一个路由规则,来生成一个action的URL  -->  
    3     <!--以下代码最后生成这样:<form action="/Index/Home/100" method="post"> 注意:按照Default2这个路由规则来生成的,所有Index在前面-->  
    4     @using (Html.BeginRouteForm("Default2",new{controller="Home",action="Index",Id=100},FormMethod.Post))  
    5     {  
    6         <input type="text" name="userName" />  
    7     }  
    8   
    9 </body> 

    下面是路由规则

     1 namespace MvcAppEF  
     2 {  
     3     public class RouteConfig  
     4     {  
     5         public static void RegisterRoutes(RouteCollection routes)  
     6         {  
     7             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
     8   
     9             routes.MapRoute(  
    10                 name: "Default",  
    11                 url: "{controller}/{action}/{id}",  
    12                 defaults: new { controller = "Test", action = "Index2", id = UrlParameter.Optional }  
    13             );  
    14   
    15             routes.MapRoute(  
    16                 name: "Default2",  
    17                 //注意:为了测试Html.BeginRouteForm,我将{action}这个占位符与{controller}占位符换了一下位置。我们来检查一下Html.BeginRouteForm最后生成什么样的url?  
    18                 url: "{action}/{controller}/{id}/{name}",  
    19                 defaults: new { controller = "Test", action = "Index2", id = UrlParameter.Optional, name = UrlParameter.Optional }  
    20             );  
    21         }  
    22     }  
    23 }  

    写法二(不推荐)

    不带参数:

    1 <body>  
    2     <!--一下写法生成:<form action="/Home/Add" method="post">-->  
    3     @{Html.BeginForm("Add", "Home");}  
    4   
    5     <input type="text" name="userName" />  
    6   
    7     @{Html.EndForm();};  <!--这段代码最终生成:</form>;-->  
    8   
    9 </body>  

    带参数:

    1 <body>  
    2     <!--一下写法生成:<form action="/Home/Add/100" method="post">-->  
    3     @{Html.BeginForm("Add", "Home", new { id=100 },FormMethod.Post);}  
    4   
    5     <input type="text" name="userName" />  
    6   
    7     @{Html.EndForm();};  <!--这段代码最终生成:</form>;-->  
    8   
    9 </body>  
  • 相关阅读:
    ceph
    分布式网关层
    function declarations are hoisted and class declarations are not 变量提升
    js为Object对象动态添加属性和值 eval c.k c[k]
    方法就是一种变量
    static 不被实例调用
    WePY根据环境变量来改变运行时的参数
    函数类型实现接口——把函数作为接口来调用
    为什么需要onRoute函数?
    504 Gateway Timeout Error 502 Bad Gateway
  • 原文地址:https://www.cnblogs.com/felix-wang/p/7324264.html
Copyright © 2011-2022 走看看