V
V
Viktor Familyevich2019-05-03 14:33:52
ASP.NET
Viktor Familyevich, 2019-05-03 14:33:52

How to pass variable from controller to view?

Good afternoon. There are variables in the controller

public static int suggestionCount { get; set; } = 3;
public static int symbolCount { get; set; } = 1;

I want to use them in the view, for this I write the following in Index.cshtml:
@{     
    var symbolCount = WebApp.Controllers.HomeController.symbolCount;
    var suggestionCount = WebApp.Controllers.HomeController.suggestionCount;
}

But var symbolCount and var suggestionCount are initialized to zero, not values ​​from the controller. Why is this happening and how to fix it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail, 2019-05-03
@makarychev13

You need to pass the model to the view. Unfortunately, a quick google only gave a link to metanit . This topic is more or less described there, but I do not recommend using it at all

W
WessBorland, 2019-07-01
@WessBorland

If the values ​​of the variables are small, you can also call @ViewBag.suggestionCount @ViewBag.symbolCount through the ViewBag
in the controller
ViewBag.suggestionCount = 3
ViewBag.symbolCount = 1
and in the view

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question