DannyD
DannyD

Reputation: 2881

is there a way that I can pass just an integer to my view without creating a model in mvc

I have a controller which calls a view. Is there a way I can pass just an integer to my view an be able to use that integer in my view with razor code?

Here is my method in my controller:

public ActionResult Details(int linkableId)
{
    return View(linkableId);
}

After returning my view, can I access just this int using razor code like this or something:

@linkableId

Upvotes: 20

Views: 24196

Answers (3)

Ali Mahmoodi
Ali Mahmoodi

Reputation: 1194

In your View, at the very top:

@model Int32

then use this in your view, should work no problem.For example:

<h1>@Model</h1>

Something else that I want to add is is your controller you should say something like this :

return View(AnIntVariable);

Upvotes: 7

Use ViewBag.

public ActionResult Details(int linkableId)
{
    ViewBag.LinkableId = linkableId;
    return View();
}

and then in your view:

@ViewBag.LinkableId 

This question may also help: How ViewBag in ASP.NET MVC works

Upvotes: 11

Lews Therin
Lews Therin

Reputation: 10995

In your View, at the very top:

@model Int32

Or you can use a ViewBag.

ViewBag.LinkableId = intval;

Upvotes: 31

Related Questions