2
votes

I am quite new with asp.net . By trying to solve problem i got this error:

System.InvalidOperationException was unhandled by user code
HResult=-2146233079 Message=Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions. Source=System.Web.Mvc
StackTrace: at System.Web.Mvc.ModelMetadata.FromLambdaExpression[TParameter,TValue](Expression`1 expression, ViewDataDictionary`1 viewData, ModelMetadataProvider metadataProvider) at System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameForInternal[TModel,TValue](HtmlHelper`1 html, Expression`1 expression, ModelMetadataProvider metadataProvider) at System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameFor[TModel,TValue](HtmlHelper`1 html, Expression`1 expression) at ASP._Page_Views_Users_Index_cshtml.Execute() in c:\Index.cshtml:line 28 at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() at System.Web.Mvc.WebViewPage.ExecutePageHierarchy() at System.Web.WebPages.StartPage.RunPage() at System.Web.WebPages.StartPage.ExecutePageHierarchy() at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
InnerException:

Main idea, That I want to represent 2 modules in my view. So I created separate class for these two modulesL

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

This is my controller where I return to my view:

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());
}

And here is my View where I want to get data of my user Login name, but I got that exception :(

    @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

How can I access to my data with lambda expressions? And how to eccess properly to these data by using class with two modules?

1
Change IEnumerable<TimeReportingLib.User> Model1 to IList<...> Model1Ondrej Svejdar

1 Answers

0
votes

Alright I figured out what was the main issue. First of all I don't need to give object name in my ActionLink, because nowhere I will use. So it is enough just to give simple string to it:

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

After that I figured out that I don't made instance of my class in my controller : UsersController.cs . So I declared that:

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

And now I can fully access to 2 modules in my view.