[英]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.