Question :
I have 2 HTML elements, a TextBox
and a Hidden
, which have values, how do I pass the values of these elements to the Action link in the Action link? Below the parameters docItemId
and vlrImobIcms
are giving error.
<div class="form-group">
@Html.Label("Valor imobilizado do ICMS", new { @class = "col-xs-4 control-label" })
<div class="col-xs-3">
@Html.TextBox("VlrImobilizado", "", new { @class = "form-control" })
</div>
<div class="col-xs-3">
@Html.ActionLink("Gerar Parcelas", "Gerar", new { bemId = @ViewBag.BemID, docItemId = ItemSelecionado.value, vlrImobIcms = VlrImobilizado.value }, new { @class = "btn btn-default btn-sm" })
@Html.Hidden("ItemSelecionado", "")
</div>
</div>
Answer :
You will have to use javascript in order to put the parameters of your input
s text
and hidden
as parameters in the link.
The value of hidden can perhaps be placed directly on the link, on the server side, if the value of the same is not changed in the browser … which would make the hidden useless in case it is never submitted through a form.
Example using jQuery
<script type="text/javascript">
$(function () {
$("#actionLinkId").on("click", function () {
var url = $(this).attr("href");
url += ((url.indexOf('?') == -1) ? '?' : '&');
url += $.param({
docItemId: $("#ItemSelecionadoId").val(),
vlrImobIcms: $("#VlrImobilizadoId").val()
});
$(this).attr("href", url);
});
})
</script>
<div class="form-group">
@Html.Label("Valor imobilizado do ICMS",
new { @class = "col-xs-4 control-label" })
<div class="col-xs-3">
@Html.TextBox("VlrImobilizado", "",
new { @class = "form-control", id = "VlrImobilizadoId" })
</div>
<div class="col-xs-3">
@Html.ActionLink("Gerar Parcelas", "Gerar",
new { bemId = @ViewBag.BemID },
new { @class = "btn btn-default btn-sm", id = "actionLinkId" })
@Html.Hidden("ItemSelecionado", "valor-qualquer",
new { id = "ItemSelecionadoId" })
</div>
</div>