我需要在Button OnClick方法中获取两个参数的值。
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" onclick="location.href='@Url.Action("ServiceDetails", "ServiceHeaders")?Remark='+ $('#txtRemarks').val(),?Cost='+ $('#txtCost').val()" />
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" onclick="location.href='@Url.Action("ServiceDetails", "ServiceHeaders")?Remark='+ $('#txtRemarks').val()'+'?Cost='+ $('#txtCost').val()" />
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" onclick="location.href='@Url.Action("ServiceDetails", "ServiceHeaders")?Remark='+ $('#txtRemarks').val() & ?Cost='+ $('#txtCost').val()" />
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" onclick="location.href='@Url.Action("ServiceDetails", "ServiceHeaders")?Remark='+ $('#txtRemarks').val():?Cost='+ $('#txtCost').val()" />
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" onclick="location.href='@Url.Action("ServiceDetails", "ServiceHeaders")?Remark='+ $('#txtRemarks').val() && ?Cost='+ $('#txtCost').val()" />
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" onclick="location.href='@Url.Action("ServiceDetails", "ServiceHeaders")?Remark='+ $('#txtRemarks').val(),?Cost='+ $('#txtCost').val()" />
我需要在Button OnClick方法的两个参数中获取txtRemarks和txtCost文本框值。
答案 0 :(得分:1)
您可以像这样通过jQuery实现您的要求
更新cshtml
<input type="text" id="txtRemarks" value="Test" />
<input type="text" id="txtCost" value="10" />
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" />
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" />
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" />
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" />
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" />
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" />
将脚本标签添加到cshtml
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function() {
updateAttr();
});
$("#txtRemarks, #txtCost").change(function() {
updateAttr();
});
function updateAttr() {
$('input[type="button"]').each(function(index, item) {
var onclick = 'location.href=';
onclick += '@Url.Action("ServiceDetails", "ServiceHeaders")';
onclick += "?Remark=" + $('#txtRemarks').val();
onclick += "?Cost=" + $('#txtCost').val();
$(item).attr('onclick', onclick);
});
}
</script>
我测试了并且有效
答案 1 :(得分:0)
我将依靠标准的ASP.NET MVC帮助器:
chtml
@using (Html.BeginForm("AddServiceHeaders", "ServiceDetails", FormMethod.Post))
{
@Html.EditorFor(model => model.Remark)
@Html.EditorFor(model => model.Cost)
<input type="button" id="btnAddtoGrid" value="Add" class="btn btn-group" />
}
c#
public sealed class Model {
public string Remark { get; set; }
public int Cost { get; set; }
}
public class ServiceDetailsController : Controller {
[HttpPost]
public ActionResult AddServiceHeaders(Model model){
// ..
}
}
我建议: