user3549602
user3549602

Reputation: 69

How to pass whole model object to mvc4 controller?

I have table on view. I want to have export to excel button. I want to pass whole model object to Controller. I have tried like this:

My view:

@model Project.Models.Results
 ...

@Html.ActionLink("Download", "DownloadAction", Model)

And my controller:

public ActionResult DownloadAction(Results model){...}

When i try it like this, model has properties with null values.

Can anybody help me to pass model to controller?

Upvotes: 0

Views: 477

Answers (2)

Abhay
Abhay

Reputation: 1

There is few ways to pass model to the view from controller. you can use TempData, ViewData or ViewBag or you can add model to your view on top of your view

@model Your_Model

and can access the value in foreach loop

@foreach(var item in Model)
{
//your data in item.something
}

OR you can pass the data to viewbag and access on view like this..

public ActionResult actionName()
{
ViewBag.ModelName=Result;
//Result in viewbag now
}

On view

@foreach(var item in ViewBag.ModelName)
{
    //your data in item.something
}

Upvotes: 0

teo van kot
teo van kot

Reputation: 12491

If you have simple model your code should work. But if you have complex model you just cant do it with ActionLink.

The only model that you can pass is model with simple properties like this:

public class ViewModel
{
   public int Id { get; set; }
   public string Name { get; set; }
   //etc...
}

Upvotes: 1

Related Questions