【问题标题】:MVC button click call POST Action methodMVC 按钮点击调用 POST Action 方法
【发布时间】:2017-10-23 12:20:51
【问题描述】:

如何从按钮单击事件调用具有复杂参数的 MVC 操作方法,如下所示?

[ValidateInput(false)]
[HttpPost]
public ActionResult Export(ViewModel vm)
{
  // some logic
}

我已将其设为 POST 操作,因为我需要将按钮所在的当前页面的 HTML 标记传递给太长的操作方法。我试过这个,但这是一个 GET 操作

<input type="button" value="Detail" onclick="location.href='@Url.Action("Export", "Report")?html=' + $('#test').html()" />

【问题讨论】:

  • 你试过把它放在提交表单中吗?
  • 您需要向该Action 发送POST 请求,即表单提交或AJAX 请求。 location.href 将发出 GET 请求
  • 厌倦使用 [ValidateInput(false)] 可能会比关闭它来完成的工作更让人头疼。
  • 使用
    "/>

标签: c# jquery html asp.net-mvc


【解决方案1】:

如果您想使用按钮单击来执行此操作,您可以订阅 JS 中按钮的单击事件。在你的 JS 中,你可以做一个 ajax 发布,它将一个 JSON 对象(你的虚拟机)发布到你的操作中:

剃须刀:

<input type="button" value="Detail" id="buttonId" />

JS:

    $('#buttonId').click(function () { //On click of your button

    var property1 = $('#property1Id').val(); //Get the values from the page you want to post
    var property2 = $('#property2Id').val();


    var JSONObject = { // Create JSON object to pass through AJAX
Property1: property1, //Make sure these names match the properties in VM
Property2: property2
};

    $.ajax({ //Do an ajax post to the controller
        type: 'POST',
        url: './Controller/Action',
        data: JSON.stringify(JSONObject),
        contentType: "application/json; charset=utf-8",
        dataType: "json"
        });

另一种方法是使用表单提交视图模型。

    @using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post)) 
{
    @Html.AntiForgeryToken()

<div class="form-horizontal">

    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <div class="form-group">
        @Html.LabelFor(model => model.PropertyName1, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <input type="text" id="PropertyName1" name="PropertyName1" class="form-control"  />
            @Html.ValidationMessageFor(model => model.PropertyName1, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.PropertyName2, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.PropertyName2, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.PropertyName2, "", new { @class = "text-danger" })
        </div>
    </div>



    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Button text" class="btn btn-primary" />
        </div>
    </div>
    </div>
}

【讨论】:

    【解决方案2】:

    试试这个

    <form action="@Url.Action("Export", "Report")" method="POST">
    <input type="hidden" name="html" id="html"  value="ADD YOUR HTML HERE">
    <input type="button" id="btn1" value="Detail" />
    </form>
    

    在js中添加脚本

    $("#btn1").click(function(){
       $("#html").val($('#test').html());
    })
    

    在你的方法中添加参数string html

    【讨论】:

      【解决方案3】:

      你可以用 HTML 表单做到这一点

      <form action="@Url.Action("Export", "Report")" method="POST">
          <input type="hidden" name="html" value="ADD YOUR HTML HERE">
          <input type="button" value="Detail" />
      </form>
      

      在你的控制器内部你需要使用 html 参数

      [ValidateInput(false)]
      [HttpPost]
      public ActionResult Export(ViewModel vm, string html)
      {
          // some logic
      }
      

      【讨论】:

      • 这将有帮助,因为第二个参数的查询字符串太长
      • 你不应该达到这个限制,当你发布你的数据时
      猜你喜欢
      • 2019-02-28
      • 2023-03-25
      • 1970-01-01
      • 2014-09-15
      • 2020-02-08
      • 2016-11-17
      • 1970-01-01
      • 1970-01-01
      • 2020-01-10
      相关资源
      最近更新 更多