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...
}
addItem('@item')