簡體   English   中英

MVC按鈕單擊調用POST操作方法

[英]MVC button click call POST Action method

如何通過按鈕單擊事件調用具有以下復雜參數的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()" />

如果要使用按鈕單擊來執行此操作,則可以訂閱JS中按鈕的單擊事件。 在您的JS中,您可以執行ajax發布,它將發布一個JSON對象(您的VM)到您的操作中:

剃刀:

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

您可以使用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
}

嘗試這個

<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());
})

在您的方法字符串html中添加參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM