How to pass parameter in an Action Link values that are in HTML elements?

Asked

Viewed 5,980 times

3

I have 2 HTML elements, a TextBox and a Hidden, which have values, how to pass the values of these elements in the Action link parameter? Below the parameters docItemId and vlrImobIcms are making a mistake.

<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>
  • do not know much of these languages, but I know HTML and POO in my opinion I think the problem is because the values "VlrImobilizado" and "ItemSelecionado" are empty and the creation of the object requires everyone to have values não nulos.

1 answer

5


You will need to use javascript to be able to put the parameters of your inputs text and hidden as parameters in link.

The value of Hidden may be placed directly on the link on the server side if the value of Hidden is not changed in the browser... which would render 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>
  • the Hidden would be a <input type=hidden> ?

  • @Pauloroberto Yes, in ASP.NET MVC it is possible to create an Hidden input like this: @Html.Hidden( ...parâmetros... ) or using even Markup.

  • Humm... and The Second Parameter of ActionLink would be its HTML ID? and the first parameter of TextBox would also be their HTML ID?

  • The second is the value, I will edit the answer, showing how to do using jQuery... then I will put ID in the elements... it is easier to explain by exemplifying.

  • @Miguelangelo posted be see your update, would be well this my suggestion. + 1, abçs

  • @Andrecalil It took a little while to update, I have little time to stay here... because I’m at work.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.