简体   繁体   English

如何将整个模型对象传递给mvc4控制器?

[英]How to pass whole model object to mvc4 controller?

I have table on view. 我在看桌子。 I want to have export to excel button. 我想导出到excel按钮。 I want to pass whole model object to Controller. 我想将整个模型对象传递给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? 有人可以帮助我将模型传递给控制器​​吗?

If you have simple model your code should work. 如果您有简单的模型,您的代码应该可以工作。 But if you have complex model you just cant do it with ActionLink . 但是,如果您具有复杂的模型,则只能使用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...
}

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 您可以使用TempData,ViewData或ViewBag,也可以在视图之上将模型添加到视图中

@model Your_Model

and can access the value in foreach loop 并可以在foreach循环中访问该值

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

OR you can pass the data to viewbag and access on view like this.. 或者,您可以将数据传递到viewbag并像这样访问视图。

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

On view 查看中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM