Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing anonymous object to view

I want to pass two values from controller action to asp.net MVC 3 Razor view. I am doing like this in action method:

var model = new { reportid = rid, refno = refernumber};
return View(model );

but when i try to access it in view like this:

@Model.reportid

I get, object doesn't contain property reportid

How can I pass multiple values without using viewbag ?

like image 280
DotnetSparrow Avatar asked Nov 02 '11 17:11

DotnetSparrow


1 Answers

Well, i strongly recommend you to use ViewModel class. But if for some reason you have phobia of extra viewmodel classes, you can use C# feature called Tuple

var model = Tuple.Create(firstObject, secondObject)

and then your view will be of type Tuple, for example

@model Tuple<int, string>

and you can access them like this

@Model.Item1

And then you can congratulate yourself, you have achieved nice mess with hidden meaning :)

like image 193
rouen Avatar answered Oct 13 '22 05:10

rouen