将对象传递到视图错误

I am getting this error when trying to pass my object to the view. I am new to MVC so please forgive me. The model item passed into the dictionary is of type 'System.Collections.Generic.List1[<>f__AnonymousType13[System.Int32,System.String,System.Nullable1[System.DateTime]]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[MvcApplication1.Models.storageProperty]'

I am trying to pass a list for a table that will show objects from the storageProperty table with the last date (if there is one) from the expenses table. Most properties have had at least one expense audit, some have had many, and others have had none. Here is the code from the controller:

var viewModel = db.storageProperties.Select(s => new
        {
            s.storagePropertyId,
            s.BuildName,
            latestExpenseSurvey = (DateTime?)s.expenses.Max(e => e.expenseDate)
        }).ToList();
        return View(viewModel); 

         }

and the @model statement in the view calls for a storageproperty object. I am using mvc3 with the entity framework. It appears obvious that I cannot pass this list object in place of the storageproperty object, but I can't figure out what to do instead, how should I do this?

Thanks in advance.

#0

Never pass anonymous objects to views. You should always pass view models.

So as always in an ASP.NET MVC application you start by defining a view model which will reflect the requirements of your view:

public class MyViewModel
{
    public int StoragePropertyId { get; set; }
    public string BuildName { get; set; }
    public DateTime? latestExpenseSurvey { get; set; }
}

Then in your controller return an IEnumerable<MyViewModel>:

public ActionResult Index()
{
    var viewModel = db.storageProperties.Select(s => new MyViewModel
    {
        StoragePropertyId = s.storagePropertyId,
        BuildName = s.BuildName,
        LatestExpenseSurvey = (DateTime?)s.expenses.Max(e => e.expenseDate)
    }).ToList();
    return View(viewModel); 
}

and finally strongly type your view to a collection of this view model:

@model IEnumerable<MyViewModel>
<div>
    @Html.EditorForModel()
</div>

#1

Your Linq query projects to an anonymous type. You need to create a named type for this projection in order to refer to it from the view.

推荐文章

如何获取组合框标签以淡入Flex

如何获取组合框标签以淡入Flex

推荐文章

将Windows窗体属性绑定到C#中的ApplicationSettings的最佳方法?

将Windows窗体属性绑定到C#中的ApplicationSettings的最佳方法?

推荐文章

我可以共享一个SQL Server Reporting Services数据源...数据集怎么办?

我可以共享一个SQL Server Reporting Services数据源...数据集怎么办?

推荐文章

C#Web服务中的加密

C#Web服务中的加密

推荐文章

在不同时区处理PHP服务器和MySQL服务器

在不同时区处理PHP服务器和MySQL服务器

推荐文章

Mono准备好迎接黄金时间了吗?

Mono准备好迎接黄金时间了吗?

推荐文章

授权所有用户仅一页的最佳做法

授权所有用户仅一页的最佳做法

推荐文章

.Net解析与转换

.Net解析与转换

推荐文章

使用C#将鼠标单击发送到任务栏中的按钮

使用C#将鼠标单击发送到任务栏中的按钮

推荐文章

将List 转换为List

将List 转换为List

推荐文章

C#:除了数据集,您还使用什么

C#:除了数据集,您还使用什么

推荐文章

从groovy中的列表创建地图的快捷方式?

从groovy中的列表创建地图的快捷方式?

推荐文章

确切的年龄计算

确切的年龄计算

推荐文章

为什么不能将Windows窗体的大小绑定到ApplicationSettings?

为什么不能将Windows窗体的大小绑定到ApplicationSettings?

推荐文章

将TDD与Web应用程序开发集成的最佳实践?

将TDD与Web应用程序开发集成的最佳实践?

推荐文章

在没有Outlook的情况下访问Exchange Server

在没有Outlook的情况下访问Exchange Server