-4

I made my website with the ViewBag, but from Java I know there are getter and setters for that. I know how I can create this getter and setter but I don't know how I can get the values in my CSHTML-File? I tried it with:

<p>Message1: @Model1.test</p>
<p>Message2: @Html.DisplayFor(model => model.test)</p>

Thanks for your help

Controller:

 public ActionResult Index()
    {
        Model1 model = new Model1();
        model.test = "Hallo Test!";
        return View();
    }

Model:

public string test { get; set; }
Mister
  • 25
  • 6
  • Possible duplicate of [Getter and Setter declaration in .NET](http://stackoverflow.com/questions/17881091/getter-and-setter-declaration-in-net) – Shakir Ahamed Dec 29 '16 at 08:49
  • What does or doesn't this code do? Please read [ask]. – CodeCaster Dec 29 '16 at 08:51
  • Show your controller method. Where you want to set your variable and where you want to get it? – teo van kot Dec 29 '16 at 08:52
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask](http://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – Nkosi Dec 29 '16 at 08:55
  • @ShakirAhamed it is not a duplicate – Ehsan Sajjad Dec 29 '16 at 08:59
  • I want just have a little mvc application where I can set a string in the controller and show it on a view. – Mister Dec 29 '16 at 09:21
  • tell view what data to show, you can use dynamic dictionaries like viewbag, tempdata, or "return view(model);" – A.T. Dec 29 '16 at 09:27

1 Answers1

0

You should create a ViewModel.And put in View method as below

 public class Model
{
    public int Id { get; set; }
}    

In your Action Method

Model model = new Model();
model.Id = 1;
return View(model);  

In you view

@model ProjectName.Model'sFolderName.Model 

You can use Model in your view now

Mehmet
  • 739
  • 1
  • 6
  • 17