0

I want to call JavaScript function, to which I pass parameter, from MVC5 Partial view.

I have declared the function addItem(item) in separate JS file at the top of the file, before $(document).ready(function {}) function.

I have tried the following code:

@foreach (string item in (TempData["Items"] as List<string>))
{
    TempData["Item"] = item;
    <script type="text/javascript">addItem(@TempData["Item"]);</script>
}

And the following:

@foreach (string item in (TempData["Items"] as List<string>))
{
    <script type="text/javascript">addItem(item);</script>
}

both tries end up with the following error: Uncaught ReferenceError: theValueOfMyItem is not defined.

I even tried this:

@foreach (string item in (TempData["Items"] as List<string>))
{
    addItem(item);
}

but as expected, this gives me error The name 'addItem' does not exist in the current context.

So my question is:

Can I call JS function with parameter from partial view, and if possible what is the proper way of doing this?

Edit 1

After the suggestions of Carsten Løvbo Andersen and Satpal I get the following error: Uncaught TypeError: addItem is not a function.

Here is how I defined the function:

function addItem(item) {
    // processing...
}
4
  • 1
    Not sure if it works but have you tried addItem('@item') Commented Jan 5, 2017 at 11:19
  • Thanks for fast replay. Still not working. Check my edit. Commented Jan 5, 2017 at 11:29
  • you want to use 'addItem(item)' function in partial view and the function is declared in MainView?? Commented Jan 16, 2017 at 10:32
  • @9-5xaero Read my answer Commented Jan 17, 2017 at 5:34

4 Answers 4

2

In your Partial View 1st convert Items List into Javascript Array and use javascript for loop to call your addItem method.

<script type="text/javascript">
    $(function(){
        var itemArray = @Html.Raw(Json.Encode(TempData["Items"] as List<string>));

        for (var item in itemsArray) {
            //call your javascript method with param item
            addItem(item);
        }
    })
</script>

another option with c# foreach loop

<script type="text/javascript">
        $(function () {
            @foreach (var item in (TempData["Items"] as List<string>))
            {   //your javascript method with param item.
                @:addItem(@item);
            }
        })
</script>

or

<script type="text/javascript">

    $(function () {
        var itemsArray = [];
        @foreach (var item in (TempData["Items"] as List<string>))
        {
            @:itemsArray.push("@item");
        }


        for (var item in itemsArray) {
            //call your javascript method with param item
            addItem(item);
        }
    })


</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Umair, but I already found different approach to solve my problem.
0

You need to pass item in quotes to be treated as string otherwise it will be treated as variables, thus the error.

<script>addItem("@item");</script>

3 Comments

What silly me, that I have missed that X.X, but still not working. Check my edit.
@9-5xaero, addItem() is not accessible
but my function is declared in global scope (as I mentioned in the beginning of my question), how is this possible?
0

In your Layout page define a section:

@RenderSection("Scripts", false)

and now you can render your code from your Partial directly to the section

<div>Some DIV</div>
@section Scripts
{
    <script>
        @foreach (string item in (TempData["Items"] as List<string>))
        {
            addItem('@item');
        }
    </script>
}
<div>Some DIV</div>
<!-- You can mix it up. Everything outside of the @section will be treated as normal markup. -->

Perhaps you mind reading this article http://www.codeguru.com/columns/dotnet/using-sections-and-partials-to-manage-razor-views.htm

3 Comments

Thank you Martin, but the foreach loop is only little part from my partial view.
The sction is just a part of your partial view. i will update my anser
Thank you Martin, but this solution still do not work for me. Never mind, I will search for another solution to my problem :)
0

What you can do is, put your data in a global variable

 `<script>
    var items = @(TempData["Items"] as List<string>); 
    foreach (var item in items)
    {
        addItem(item);
    }
  </script>`

1 Comment

Thank you SoftwareNerd, but the provided sample do not work. Never mind, I will search for another solution to my problem :)

Your Answer

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