C# MVC按钮单击调用后操作方法
如何从按钮单击事件调用具有复杂参数的MVC操作方法,如下图所示C# MVC按钮单击调用后操作方法,c#,jquery,html,asp.net-mvc,C#,Jquery,Html,Asp.net Mvc,如何从按钮单击事件调用具有复杂参数的MVC操作方法,如下图所示 [ValidateInput(false)] [HttpPost] public ActionResult Export(ViewModel vm) { // some logic } 我将其作为一个POST操作,因为我需要将按钮所在的当前页面的HTML标记传递给操作方法,因为操作方法太长。我试过这个,但这是一个GET手术 <input type="button" value="Detail" onclick="loca
[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()" />
您可以使用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>
试试这个
<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>
在方法中添加参数字符串html如果您想使用按钮单击来执行此操作,您可以在JS中订阅按钮的单击事件。在JS中,您可以执行ajax post,它将JSON对象(您的VM)发布到您的操作: 剃刀:
<input type="button" value="Detail" id="buttonId" />
另一种方法是使用表单提交视图模型
@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.BeginForm(“ActionName”、“ControllerName”、FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.PropertyName1,htmlAttributes:new{@class=“controllabel col-md-2”})
@Html.ValidationMessageFor(model=>model.PropertyName1,“,new{@class=“text danger”})
@LabelFor(model=>model.PropertyName2,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.PropertyName2,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.PropertyName2,“,new{@class=“text danger”})
}
您是否尝试将其置于提交表单中?您需要向该操作发送POST请求,即表单提交或AJAX请求location.href
将使GET请求厌倦使用[ValidateInput(false)]可能会比您试图通过关闭[ValidateInput(false)]来完成的任务更让您头疼。您可以检查:使用此选项将有帮助,因为查询字符串距离第二个参数太长。发布数据时,您不应该达到该限制
<input type="button" value="Detail" id="buttonId" />
$('#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>
}