MVC |表单提交 Html.BeginForm

用户提交表单

写法一(推荐)

一,不带参数


    
    @using (Html.BeginForm("Add","Home",new{ id=200}))
    {
        
    }


二,带参数


    
    @using (Html.BeginForm("Add","Home",new{ id=200,pric=25},FormMethod.Get))
    {
        
    }


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

带参数:


    
    
    @using (Html.BeginRouteForm("Default2",new{controller="Home",action="Index",Id=100},FormMethod.Post))
    {
        
    }

不带参数


    
    
    @using (Html.BeginRouteForm("Default2"))
    {
        
    }

下面是路由规则

namespace MvcAppEF
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Test", action = "Index2", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "Default2",
                //注意:为了测试Html.BeginRouteForm,我将{action}这个占位符与{controller}占位符换了一下位置。我们来检查一下Html.BeginRouteForm最后生成什么样的url?
                url: "{action}/{controller}/{id}/{name}",
                defaults: new { controller = "Test", action = "Index2", id = UrlParameter.Optional, name = UrlParameter.Optional }
            );
        }
    }
}



写法二(不推荐)


    
    @{Html.BeginForm("Add", "Home");}

    

    @{Html.EndForm();};  


还可以带参数:


    
    @{Html.BeginForm("Add", "Home", new { id=100 },FormMethod.Post);}

    

    @{Html.EndForm();};  



你可能感兴趣的:(Mvc)