-1

I have a issue in Visual Studio 2017. I want to get data from the controller in the Razor file. So the value doesn't exist in the file.

The code and problem is below.

Controller:

public IActionResult Product(int? product)
{
    if (product == 0)
    {
        return RedirectToAction("index", "home");
    }
    else
    {
        ViewBag.product = product;
        return View();
    }
}

scripts:

@section Scripts{
    <script>
        var pr = @ViewBag.product;
    </script>
}

Error: enter image description here

Some links of what I haved tried in order to fix it: question1 question2

Palle Due
  • 5,929
  • 4
  • 17
  • 32
vankhanhpr
  • 203
  • 2
  • 9

2 Answers2

2

Place value in quotes:

var pr = '@ViewBag.product';
Backs
  • 24,430
  • 5
  • 58
  • 85
0

At first, make sure that value is set in ViewBag.product in controller.

CodesDDecodes
  • 132
  • 1
  • 15