zoukankan      html  css  js  c++  java
  • 视图表单访问控制器操作方法的POST、GET方式对应关系

    在视图中,表单默认访问方式是FormMethod.Post(不会将请求显示在地址栏中)。在控制器中,操作方法不标注属性,默认为HttpGet属性。会有以下情况出现。

    1、表单不指定访问方式(默认形式为Post),只有一个操作方法,且不标注属性,默认为HttpGet属性。则表单将数据提交至控制器HttpGet方法中。
     @using (Html.BeginForm())
        {
           <div class="form-group">
               <label for="searchString" class="control-label">片名:</label>
           </div>
             <div class="form-group">
                @Html.TextBox("searchString", "", htmlAttributes: new { @class = "form-control", placeholder = "请输入片名" })
            </div>
                <input type="submit" value="查找" class="btn btn-primary" />
        }

    2、表单不指定访问方式(默认形式为Post),控制器操作方法有两个重载,分别 为[HttpGet]和[HttpPost]属性。则表单将数据提交至控制器HttpPost方法中。

    3、表单指定数据发送形式为FormMethod.Get,它能使Post请求能够路由到[HttpGet]版本(默认的)的操作方法中,请求的地址也能够在URL地址栏显示。View中Form形式如下:@using (Html.BeginForm("Index","Movies",FormMethod.Get))

      @using (Html.BeginForm("Index", "Movie", FormMethod.Get, htmlAttributes: new { @class = "form-inline", role = "form" }))
        {
           <div class="form-group">
               <label for="searchString" class="control-label">片名:</label>
           </div>
             <div class="form-group">
                @Html.TextBox("searchString", "", htmlAttributes: new { @class = "form-control", placeholder = "请输入片名" })
            </div>
                <input type="submit" value="查找" class="btn btn-primary" />
        }

    总之,控制器[HttpGet]属性的操作方法能接受GET、POST形式发送的表单值。而控制器[HttpPost]属性的操作方法只能接受POST形式的表单。

    Get表单只能发送[HttpGet]属性的操作方法中;POST表单能将数据发送至[HttpGet]、[HttpPost]属性的操作方法中,但优先访问[HttpPost]属性的操作方法。

    一般说来,1、如果操作方法一般是不改变程序状态的,也就是不增删改(只查询)数据库的操作。使用FormMethod.Get表单,形式为(@using (Html.BeginForm("Index","Movies",FormMethod.Get))),[HttpGet]属性(默认的样式,不设置)的操作方法。2、如果涉及到数据库的增删、改操作,使用POST表单,默认的样式,形式如@using(html.BeginForm()),[HttpPost]属性的操作方法。

  • 相关阅读:
    query.setXXX预编译赋值 (坑爹的)
    JAVA 预编译执行SQL 之setparameterList用法
    ActiveMQ
    JavaScript DOM日记
    Mysql 详解(三)
    Mysq连接使用
    Mysql详解(二)
    Mysql详解(一)
    Spring MVC 批量导入Excel文件
    《走遍中国》珍藏版(三)
  • 原文地址:https://www.cnblogs.com/liuyuanhao/p/4319343.html
Copyright © 2011-2022 走看看