How to pass a variable in a Javascript function by the onclick Razor?

Asked

Viewed 1,976 times

0

I have a series of fields I need to send from tag Razor for a function JavaScript

@Html.ActionLink("Enviar", "ActionName", null, new {
    onclick = String.Format("envia_pessoa({0}, '{1}', '{2}', '{3}', '{4}', '{5}', '{6}')", @item.ID, @item.Nome, @item.Endereco, @item.CEP, @item.Cidade, @item.UF)
});

However, I cannot send these variables to função JavaScript. Could someone help me?

Error:

Index (based on zero) must be greater than or equal to zero and smaller than the argument list size.

  • How it’s generating HTML?

  • The code is giving the following error: Index (based on zero) must be greater than or equal to zero and smaller than the length of the argument list. In fact I assumed that the code is implemented in this way.

  • Post your javascript function

  • I found the problem, posted there

  • If possible mark the answer as the right answer to assist later users

2 answers

1


I figured out the mistake, you’re going through 7 parameters in the String.Format and only 6 values at the end

You have to change

onclick = String.Format("envia_pessoa({0}, '{1}', '{2}', '{3}', '{4}', '{5}', '{6}')", @item.ID, @item.Nome, @item.Endereco, @item.CEP, @item.Cidade, @item.UF)

To

onclick = String.Format("envia_pessoa({0}, '{1}', '{2}', '{3}', '{4}', '{5}')", @item.ID, @item.Nome, @item.Endereco, @item.CEP, @item.Cidade, @item.UF)

That way you both get 6 values.

  • My html tag is coming back with onclick in front of href. I have another problem that is making me break my head: How do I get the routen value Object from the page and send it to java script?

  • @Germanosampaio what exactly you need?

  • The result of my query should return to another page for corresponding fields. for this I am using javascript to put the returned fields of the query to the fields of the main screen. This may be easy, but I’m new to Razor and Javascript. Opened a new post with all the problems: http://answall.com/questions/67919/consulta-n-returneds-para-partial-view-e-o-returnion-javascript-n%C3%A3o-returns them

0

In my solution, I have a button that opens a query modalview. After the query, I need to return the result of the records in the form of a link. When clicking on the link, I must return to the main screen the result of the query but before changing and directing the fields to one or more that receive the same reference. I’m having two problems: The first one is that I’m unable to return the result of the query in Partialview Modal, and the second one when I send the return of the query, in enters the Javascript method that redirects the fields. Could someone help me? Follow the short code: View of Notes registration:

Banknote view

@Html.EditorFor(model => model.NOTA_NOM_DEV, new { htmlAttributes = new { @class = "form-control", value = "", size = "45", maxlength = "45", type = "hidden" } })

@Html.ActionLink("Fornecedor", "_ConPessoas", "Pessoas", new { id = 4 }, new { @class = "modal-link btn btn-default" , id = '4'})

<script type="text/javascript">
    $(function () {
        // Initialize numeric spinner input boxes
        //$(".numeric-spinner").spinedit();

        // Initalize modal dialog
        // attach modal-container bootstrap attributes to links with .modal-link class.
        // when a link is clicked with these attributes, bootstrap will display the href content in a modal dialog.
        $('body').on('click', '.modal-link', function (e) {
            e.preventDefault();
            $(this).attr('data-target', '#modal-container');
            $(this).attr('data-toggle', 'modal');
        });

        // Attach listener to .modal-close-btn's so that when the button is pressed the modal dialog disappears
        $('body').on('click', '.modal-close-btn', function () {
            $('#modal-container').modal('hide');
        });

        //clear modal cache, so that new content can be loaded
        $('#modal-container').on('hidden.bs.modal', function () {
            $(this).removeData('bs.modal');
        });

        $('#CancelModal').on('click', function () {
            return false;
        });

    });
</script>

_Partial view de Consulta
@{ Layout = null; } @using PagedList.Mvc; @*@using SisCerinfo.Models;*@ @using (Html.BeginForm("_ConPessoas", "Notas", FormMethod.Get)) {  
Pesquisa de Pessoas
CPF/CNPJ: @*@Html.EditorFor(model => model.PES_DOCID, new { htmlAttributes = new { @class = "form-control", maxlength = "14", size = "14" } })*@ ou Nome: @*@Html.EditorFor(model => model.PES_NM, new { htmlAttributes = new { @class = "form-control", maxlength = "50", size = "50" } })*@

@*@Html.ActionLink("Devedor ", "ViewConPes", "Notas", null, new { @class = "modal-link btn btn-success" })*@
@{ if (Model != null) { foreach (var item in Model) { @item.PES_NM @Html.ActionLink("Enviar", "CadNotas", null, new { onclick = String.Format("informa_pessoa({0}, '{1}', '{2}'", @item.PES_ID,'0','0')}) } } } } $(function () { $('#approve-btn').click(function () { $('#modal-container').modal('hide'); }); }); function informa_pessoa(pesid, sus_lim, sus_dt) { //var pessoa = document.forms[0].id var pessoa = HttpContext.Current.Request.RequestContext.RouteData.Values["id"].ToString() //var pessoa = document.frm.TipoPesquisa.value; //var pessoa = document.frm.TipoPesquisa.value; if (pessoa == 4) { window.opener.document.frm.NOTA_NOM_DEV.value = trim(pesnm); window.opener.document.frm.NOTA_LIM.value = trim(sus_lim); window.opener.document.frm.NOTA_DT.value = trim(sus_dt); } window.close(); } 

Browser other questions tagged

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