Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

asp.net-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
like image 893
DannyD Avatar asked Aug 07 '13 14:08

DannyD


People also ask

How do you pass model data into a view?

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.

Which methods that can be used to pass data from the controller to the view?

ViewData, ViewBag, and TempData are used to pass data between controller, action, and views. To pass data from the controller to view, either ViewData or ViewBag can be used.

How can you pass an instance of a class to an action method?

There are a number of ways in which you can pass parameters to action methods in ASP.NET Core MVC. You can pass them via a URL, a query string, a request header, a request body, or even a form.

How many types of views are there in MVC?

On basis of data transfer mechanism ASP.NET MVC views are categorized as two types, Dynamic view. Strongly typed view.


3 Answers

In your View, at the very top:

@model Int32

Or you can use a ViewBag.

ViewBag.LinkableId = intval;
like image 82
Lews Therin Avatar answered Oct 23 '22 06:10

Lews Therin


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

like image 11
Fiona - myaccessible.website Avatar answered Oct 23 '22 06:10

Fiona - myaccessible.website


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);
like image 7
Ali Mahmoodi Avatar answered Oct 23 '22 07:10

Ali Mahmoodi