1

so for pagination I have this url that when a user press the link it goes to the next page (the link parts are in a partial view), for that to work i get the dropdowbox value with javascript and pass to the url

 @Html.ActionLink("|Siguiente >", "Index", new { pagina = Model.PageNumber + 1, ordenacion = ViewBag.Ordenacion, filtro = ViewBag.Filtro , empresa = "param-empresa" }, new { id = "mylinkSig" })



<script type="text/javascript">
     $(function () {
            $('#mylinkSig').click(function () {
                var empresa = $("#empresa").val();
                this.href = this.href.replace("param-empresa", encodeURIComponent(empresa));
            });
        });
</script> 

as I use the script in almost all my page i want to put this script in a js file so i dont have to write it in all my view (pages) y try to copy/paste and put in a js file and it dont work (yes i have the .js file refence in my page)

so iam new to javascript so i dont know if i have to change the function for it could work in the .js file an use it in all my page

edit:

my Helper.js

$(function () {
    $('#mylinkSig').click(function () {
        var empresa = $("#empresa").val();
        this.href = this.href.replace("param-empresa", encodeURIComponent(empresa));
    });
});


function deleteConfirmation(response, status, data) {

    // remove the row from the table
    var rowId = "#widget-id-" + response.id;
    $('.widgets').find(rowId).remove();

    // display a status message with highlight
    $('#actionMessage').text(response.message);
    $('#actionMessage').effect("highlight", {}, 3000);
    if (response.message == null) {
        alert('No se pudo eliminar el registro');
    }
    else {
        alert(response.message);
    }

}

my view

<script src="@Url.Content("~/Scripts/Helper.js")" type="text/javascript"></script>

&nbsp;
@*navigation << < >>>*@
<div>

    Pagina @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
    de @Model.PageCount
    &nbsp;
    @if (Model.HasPreviousPage)
    {
        @Html.ActionLink("<<|", "Index", new { pagina = 1, ordenacion = ViewBag.Ordenacion, filtro = ViewBag.Filtro, empresa = "param-empresa" }, new { id = "mylinkFirst" })
        @Html.Raw("&nbsp;");
        @Html.ActionLink("< Anterior|", "Index", new { pagina = Model.PageNumber - 1, ordenacion = ViewBag.Ordenacion, filtro = ViewBag.Filtro, empresa = "param-empresa" }, new { id = "mylinkAnt" })
    }
    &nbsp;
    @if (Model.HasNextPage)
    {
        @Html.ActionLink("|Siguiente >", "Index", new { pagina = Model.PageNumber + 1, ordenacion = ViewBag.Ordenacion, filtro = ViewBag.Filtro , empresa = "param-empresa" }, new { id = "mylinkSig" })
        @Html.Raw("&nbsp;");
        @Html.ActionLink("|>>", "Index", new { pagina = Model.PageCount, ordenacion = ViewBag.Ordenacion, filtro = ViewBag.Filtro , empresa = "param-empresa"}, new { id = "mylinkLast" })
    }

</div>

&nbsp;
1
  • 2
    You generally don't have to change JS to put it in a separate .js file (don't include the <script> tags though). What you may find is that you may need to attach it to the onLoad event of the body to make sure the page has loaded before it executes (depending on what you need to do). Commented Jul 17, 2012 at 15:43

1 Answer 1

1

If you have a reference to the file, then my best guess is that you are copying the script-tag into the JS-file as well, which you shouldn't.

Try just copying this part:

$(function () {
    $('#mylinkSig').click(function () {
        var empresa = $("#empresa").val();
        this.href = this.href.replace("param-empresa", encodeURIComponent(empresa));
    });
});

If you do that, there shouldn't be a difference between having the code within the page, compared to having it in a separate file.

Sign up to request clarification or add additional context in comments.

5 Comments

no iam nop copy with that the posible is that i have another function in that .js file function deleteConfirmation(response, status, data) { //do a delete }
@Diego_DX What does that code look like? It sounds like you might have a syntax-error or something in that code, that breaks the execution. What does your console tell you, do you get any exceptions?
dont get exception it work fine when it is in the same page. but when i put the function in the js.file don't work when i press the link nothing happens
@Diego_DX Well, I will have to see some more code to troubleshot this. Could you provide your complete code for the JS-file, and perhaps the script tag that reference it. I bet the problem lie within one of those.
i have the function deleteConfirmation in my .js file it work fine, so i don't know why the other do not work when i put it in my .js file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.