在View中使用2个模块:System.Web.Mvc.dll中发生类型为“System.InvalidOperationException”的异常,但未在用户代码

时间:2016-04-07 06:25:26

标签: c# .net asp.net-mvc foreach lambda

我是asp.net的新手。通过尝试解决问题我得到了这个错误:

  

System.InvalidOperationException未被用户代码
处理   HResult = -2146233079 Message =模板只能用于字段   访问,属性访问,单维数组索引或   单参数自定义索引器表达式。来源= System.Web.Mvc
  堆栈跟踪:          在System.Web.Mvc.ModelMetadata.FromLambdaExpression [TParameter,TValue](Expression`1   表达式,ViewDataDictionary`1 viewData,ModelMetadataProvider   metadataProvider)          在System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameForInternal [TModel,TValue](HtmlHelper`1   html,Expression`1表达式,ModelMetadataProvider metadataProvider)          在System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameFor [TModel,TValue](HtmlHelper`1   html,表达式`1表达式)          在ASP._Page_Views_Users_Index_cshtml.Execute()在c:\ Index.cshtml:第28行          在System.Web.WebPages.WebPageBase.ExecutePageHierarchy()          在System.Web.Mvc.WebViewPage.ExecutePageHierarchy()          在System.Web.WebPages.StartPage.RunPage()          在System.Web.WebPages.StartPage.ExecutePageHierarchy()          在System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext   pageContext,TextWriter writer,WebPageRenderingBase startPage)          在System.Web.Mvc.RazorView.RenderView(ViewContext viewContext,TextWriter writer,Object instance)          在System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext,TextWriter writer)          在System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)          在System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext   controllerContext,ActionResult actionResult)          在System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1   filters,Int32 filterIndex,ResultExecutingContext preContext,   ControllerContext controllerContext,ActionResult actionResult)          在System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1   filters,Int32 filterIndex,ResultExecutingContext preContext,   ControllerContext controllerContext,ActionResult actionResult)
  的InnerException:

主要想法,我想在我看来代表2个模块。所以我为这两个模块创建了单独的类

public class TimeReportViewMod
{
    public IEnumerable<TimeReportingLib.User> Model1 { get; set; }
    public IPagedList<TimeReportingLib.User> Model2 { get; set; }
}

这是我的控制器,我回到我的观点:

public ActionResult Index(string sortOrder, string currentFilter, string searchString,  int? page)
{
    ViewBag.CurrentSort = sortOrder;
    ViewBag.NameSortParm = String.IsNullOrWhiteSpace(sortOrder) ? "login_desc" : "";

    var users = from s in db.Users.Include(u => u.CustomerProject).Include(u => u.Service).Include(u => u.Customer) select s;
    //var sortUsers = from s in db.Users select s;

    if(searchString != null)
    {
        page = 1;
    }
    else
    {
        searchString = currentFilter;
    }
    ViewBag.CurrentFilter = searchString;
    if (!String.IsNullOrEmpty(searchString))
    {
        users = users.Where(s => s.Login.Contains(searchString) || s.UserName.Contains(searchString));
    }
    switch (sortOrder)
    {
        case "login_desc":
            users = users.OrderByDescending(s => s.Login);
            break;
        default:
            users = users.OrderBy(s => s.Login);
            break;
    }
    int pageSize = 3;
    int pageNumber = (page ?? 1);
    return View("Index",new TimeReportViewMod());
}

这是我的视图,我想获取用户登录名的数据,但我得到了例外:(

    @model TimeReportingWebApp.TimeReportViewMod
    @using PagedList.Mvc;
    <link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
    @{
        ViewBag.Title = "Users";
    }
    <h2>Users</h2>
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    @using (Html.BeginForm("Index", "Users", FormMethod.Get))
    {
        <p>
            Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string) 
            <input type="submit"  class="btn btn-primary" value="Search" />
        </p>
    }
    <table class="table">
        <tr>
            <th>
                @Html.ActionLink(Html.DisplayNameFor(m => m.Model1.Select(x => x.Login)).ToHtmlString(), "Index")
            </th>
        </tr>
@foreach (var item in Model.Model1) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Login)
        </td>
</tr>
    </table>
    Page @(Model.Model2.PageCount < Model.Model2.PageNumber ? 0 : Model.Model2.PageNumber) of @Model.Model2.PageCount

如何使用lambda表达式访问我的数据?如何通过使用具有两个模块的类来正确地访问这些数据?

1 个答案:

答案 0 :(得分:0)

好吧我弄清楚主要问题是什么。 首先,我不需要在ActionLink中提供对象名称,因为我无处可用。所以仅仅为它提供简单的字符串就足够了:

@Html.ActionLink("Login","Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })

之后我发现我没有在我的控制器中创建我的类的实例:UsersController.cs。所以我宣布:

var inst = new TimeReportViewMod();
inst.Model1 = users.ToList();
inst.Model2 = users.ToPagedList(pageNumber, pageSize);    
return View("Index", inst);

现在我可以在我看来完全访问2个模块。