简体   繁体   English

MVC按钮单击调用POST操作方法

[英]MVC button click call POST Action method

How can I call an MVC action method with complex parameters, like the below, from a button click event? 如何通过按钮单击事件调用具有以下复杂参数的MVC操作方法?

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

I have made it a POST action because I need to pass HTML tags of the current page on which the button is to the action method which is too long. 之所以将它设为POST动作,是因为我需要将按钮所在的当前页面的HTML标签传递给操作方法,该方法太长。 I have tried this but this its for a GET operation 我已经尝试过了,但这是针对GET操作的

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

If you want to do this using a button click you can subscribe the to click event of the button in JS. 如果要使用按钮单击来执行此操作,则可以订阅JS中按钮的单击事件。 In your JS, you can do an ajax post, which will post a JSON object (your VM) to your action: 在您的JS中,您可以执行ajax发布,它将发布一个JSON对象(您的VM)到您的操作中:

Razor: 剃刀:

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

JS: 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"
        });

Another way to do this is submit the view model using a form. 另一种方法是使用表单提交视图模型。

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

You can do that with HTML form 您可以使用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>

And inside your controller you need to use html parameter 在控制器内部,您需要使用html参数

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

try this 尝试这个

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

add Script in js 在js中添加脚本

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

add param in your method string html 在您的方法字符串html中添加参数

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

相关问题 单击按钮调用MVC控制器和操作方法 - Call MVC Controller and action method on button click 如何在AVC中使用ajax进行按钮单击来调用Action方法? - How to call an Action method using ajax for a button click which is in partial view in MVC? 将ViewModel值传递给按钮或ActionLink单击上的MVC Controller操作方法 - Passing ViewModel values to MVC Controller action method on button or ActionLink click 在MVC5中使用表单身份验证时,以不同的操作方法触发按钮单击 - On using formsauthentication in MVC5, button click is firing in different action method 在MVC中如何在单击按钮时移动到其他动作方法 - How to move to other action method on button click in MVC MVC 5 将单选按钮字符串值发送到发布操作方法 - MVC 5 send radio button string value to post action method ASP.NET MVC Knockout 按钮单击调用 POST API - ASP.NET MVC Knockout button click call POST API 使用 Url.Action 调用按钮单击的方法 - call method on button click using Url.Action 在单击HTML按钮(不提交或表单的帖子按钮)Asp.net MVC上调用控制器的特定操作 - Calling a specific action of a controller on the click of HTML button(Not Submit or Form's post Button) Asp.net MVC 如何在 MVC 中使用按钮调用方法 - How to call a method with a button in MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM