本文介绍了如何添加一个"模型"下拉列表中IPagedlist模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面来显示每个日志(消息,时间,类型,客户ID,名称的)在HTML表格。由于日志是巨大的。我正在使用的剃刀MVC一个IPagedList,这完美的作品。我现在有2个搜索框(适用于管理员)和1个会员。在这里您可以通过邮件和客户ID进行搜索。

现在的问题是,我不希望用户只是有一个文本框,在这里你不仅可以把一个数字(例如你把客户ID 2,并得到了客户T) - 而是我想要一个下拉与目前所有的客户名称连接到客户ID。

我在另一页我用这个功能,但它只能是因为我返回其他网页上的型号和因为日志页面返回IPagedListModel,而不是一个示范我无法使用此解决方案。我怎么会做这种解决方案的工作为我的日志页呢?

HTML code

  @:其中,P&​​GT; @ Html.DropDownListFor(M = GT; m.SelectedCustomer,Model.CustomerList)LT; / P>

型号

 使用系统;
使用System.Linq的;
使用System.ComponentModel.DataAnnotations;
使用System.ComponentModel.DataAnnotations.Schema;
使用System.Data.Entity的;
使用System.Collections.Generic;
使用PagedList;
使用System.Web.Mvc;命名空间ASDMVC.Models
{
    [表(日志)]
    公共类LogModel
    {
        [键]
        众长ID {搞定;组; }
        公共字符串消息{搞定;组; }
        公共DateTime时间戳{搞定;组; }
        公共字符串水平{搞定;组; }
        公众诠释客户ID {搞定;组; }
        [NotMapped]
        公共字符串名称{;组; }
    }    公共类LogModelVM
    {
        公众诠释SelectedCustomer {搞定;组; }
        公共IEnumerable的< SelectListItem> CustomerList {搞定;组; }
        公共字符串消息{搞定;组; }
        公共IPagedList< LogModel>日志{搞定;组; }
    }    公共类LogDBContext:的DbContext
    {
        公共LogDBContext():基地(的MySqlConnection)
        {        }        公共DbSet< LogModel>登录{搞定;组; }        公众的IQueryable< LogModel> GetLogs()
        {
            从登录登录返回
                   选择登录;
        }
    }
}

控制器

 公共类DbCustomerIds
{
    公开名单< D​​bCustomer> GetCustomerIds()
    {
        清单< D​​bCustomer>客户=新的List< D​​bCustomer>();
        字符串的queryString =SELECT * FROM dbo.customers
        SqlDataAdapter的适配器=新SqlDataAdapter的(的queryString,System.Configuration.ConfigurationManager.ConnectionStrings [的MySqlConnection]的ConnectionString);
        数据集的客户=新的DataSet();
        adapter.Fill(客户,客户);        的foreach(在customers.Tables DataRow的项目[0] .Rows)
        {
            DbCustomer CID =新DbCustomer();
            cid.FakeId = Convert.ToInt32(项目[ID]);
            cid.FakeName = Convert.ToString(项目[名称]);
            Customers.Add(CID);
        }
        回报客户;
    }
}私人的IEnumerable< SelectListItem> GetCustomerIds()
{
    VAR DbCustomerIds =新DbCustomerIds();
    VAR的客户= DbCustomerIds
                    .GetCustomerIds()
                    。选择(X =>
                            新SelectListItem
                            {
                                值= x.FakeId.ToString(),
                                文字= x.FakeName
                            });
        返回新的SelectList(客户,值,文本);
    }
}[HttpPost]
    公共PartialViewResult LogPartialView(字符串搜索字符串,字符串searchString2,串currentFilter,串currentFilterz,诠释?页,串中将sortOrder)
    {
        如果(Roles.IsUserInRole(WebSecurity.CurrentUserName,管理))
        {
            顾客= GetCustomerIds();
            消息= db.GetLogs();
            INT的pageSize = 10;
            INT PAGENUMBER =(第53页1);
            VAR日志= ​​message.OrderByDescending(I => i.timeStamp).ToPagedList(PAGENUMBER,pageSize的);
            的foreach(VAR日志中的日志)
                log.Name = Customer.Where(A => a.value中== log.customerId.ToString())。FirstOrDefault()文本;
        LogModelVM LMVM =新LogModelVM();
        LMVM.Logs =日志;
        LMVM.CustomerList =客户;
        返回PartialView(_ LogPartialLayout,LMVM);
    }
LogModelVM LMVM =新LogModelVM();
        LMVM.Logs =日志;
        LMVM.CustomerList =客户;
        返回PartialView(_ LogPartialLayout,LMVM);
}

_LogPartialLayout

  @model ASDMVC.Models.LogModelVM
@using PagedList.Mvc;
<链接HREF =〜/内容/ PagedList.css的rel =stylesheet属性类型=文/ CSS/>@if(Roles.IsUserInRole(WebSecurity.CurrentUserName,管理))
{
    <表类=表>
        &所述; TR>
            百分位的id =tableth>
                信息            < /第i
            百分位的id =tableth>
                时间戳
            < /第i
            百分位的id =tableth>
                水平
            < /第i
            百分位的id =tableth>
                客户ID
            < /第i
            百分位的id =tableth>
                顾客姓名
            < /第i
        < / TR>
        @foreach(在Model.Logs VAR项)
        {
            &所述; TR>
                &所述; TD>
                    @ Html.DisplayFor(modelItem => item.message)
                < / TD>
                &所述; TD>
                    @ Html.DisplayFor(modelItem => item.timeStamp)
                < / TD>
                &所述; TD>
                    @ Html.DisplayFor(modelItem => item.level)
                < / TD>
                &所述; TD>
                    @ Html.DisplayFor(modelItem => item.customerId)
                < / TD>
                &所述; TD>
                    @ Html.DisplayFor(modelItem => item.Name)
                < / TD>
            < / TR>
        }
    < /表>
}
@if(Roles.IsUserInRole(WebSecurity.CurrentUserName,会员))
{
    <表类=表>
        &所述; TR>
            百分位的id =tableth>
                信息            < /第i
            百分位的id =tableth>
                时间戳
            < /第i
            百分位的id =tableth>
                水平
            < /第i        < / TR>        @foreach(在Model.Logs VAR项)
        {
            &所述; TR>
                &所述; TD>
                    @ Html.DisplayFor(modelItem => item.message)
                < / TD>
                &所述; TD>
                    @ Html.DisplayFor(modelItem => item.timeStamp)
                < / TD>
                &所述; TD>
                    @ Html.DisplayFor(modelItem => item.level)
                < / TD>
            < / TR>
        }
    < /表>
}页@(Model.Logs.PageCount< Model.Logs.PageNumber 0:Model.Logs.PageNumber)的@ Model.Logs.PageCount
@ Html.PagedListPager(Model.Logs,页=> Url.Action(LogPartialView
    新{页面中将sortOrder = ViewBag.CurrentSort,currentFilter = ViewBag.CurrentFilter,currentFilterz = ViewBag.CurrentFilterz}) PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(PagedListRenderOptions.ClassicPlusFirstAndLast,
        新AjaxOptions
        {
            InsertionMode = InsertionMode.Replace,
            列举HTTPMethod =POST
            UpdateTargetId =divLogs
        }))

任何帮助将是AP prechiated,对于长的问题对不起 - 我只是想获得所有这些似乎有关情况的信息

先谢谢了。

运行时电流误差:

解决方案

Create a view model with the properties you need in the view

public class LogModelVM
{
  public int SelectedCustomer { get; set; }
  public IEnumerable<SelectListItem> CustomerList { get; set; } // suggested name change
  public string Message { get; set; } // for the message search box?
  public IPagedList<NowasteWebPortalMVC.Models.LogModel> Logs { get; set; }
  .... // add properties for sortorder etc
}

Then in the controller method, initiaize a new LogModelVM and assign the values (e.g. model.Logs = logs;), and return the view model so that in the view you can use

@model yourAssembly.LogModelVM
....
@Html.DropDownListFor(m => m.SelectedCustomer, Model.CustomerList) // why change the id attribute?
....
@Html.PagedListPager(Model.Logs, page => Url.Action(...`

You should also consider adding the other properties such as sortOrder and currentfilter rather than using ViewBag

Side note: Ensure that all associated views, including the main view also use @model yourAssembly.LogModelVM

这篇关于如何添加一个&QUOT;模型&QUOT;下拉列表中IPagedlist模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 01:53