如何添加一个"模型"下拉列表中IPagedlist模式? [英] How do I add a "model" dropdown in a IPagedlist model?
问题描述
我有一个页面来显示每个日志(消息,时间,类型,客户ID,名称的)在HTML表格。由于日志是巨大的。我正在使用的剃刀MVC一个IPagedList,这完美的作品。我现在有2个搜索框(适用于管理员)和1个会员。在这里您可以通过邮件和客户ID进行搜索。
现在的问题是,我不希望用户只是有一个文本框,在这里你不仅可以把一个数字(例如你把客户ID 2,并得到了客户T) - 而是我想要一个下拉与目前所有的客户名称连接到客户ID。
我在另一页我用这个功能,但它只能是因为我返回其他网页上的型号和因为日志页面返回IPagedListModel,而不是一个示范我无法使用此解决方案。我怎么会做这种解决方案的工作为我的日志页呢?
HTML code
@:其中,P> @ 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
{
公开名单< DbCustomer> GetCustomerIds()
{
清单< DbCustomer>客户=新的List< DbCustomer>();
字符串的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,对于长的问题对不起 - 我只是想获得所有这些似乎有关情况的信息
先谢谢了。
运行时电流误差:
[出现InvalidOperationException:传递到字典的模型项的类型是PagedList.PagedList`1 [NowasteWebPortalMVC.Models.LogModel]',但本词典需要类型的模型项目NowasteWebPortalMVC.Models.LogModelVM']
块引用>解决方案创建您可以在视图所需要的属性视图模型
公共类LogModelVM
{
公众诠释SelectedCustomer {搞定;组; }
公共IEnumerable的< SelectListItem> CustomerList {搞定;组; } //建议更名
公共字符串消息{搞定;组; } //为消息搜索框?
公共IPagedList< NowasteWebPortalMVC.Models.LogModel>日志{搞定;组; }
.... //用于排序顺序等添加属性
}然后在控制器方法,initiaize一个新的
LogModelVM
并指定值(例如model.Logs =日志;
),并返回视图模型,以便在视图中可以使用@model yourAssembly.LogModelVM
....
@ Html.DropDownListFor(M = GT; m.SelectedCustomer,Model.CustomerList)//为什么更改其ID属性?
....
@ Html.PagedListPager(Model.Logs,页=> Url.Action(...`您也应该考虑增加其他属性,如
中将sortOrder
和currentfilter
而不是使用ViewBag
边注:确保所有相关意见,包括主视图中也使用
@model yourAssembly.LogModelVM
I have a page to display every log (message, time, type, customerId, Name) in a html table. Since the log is huge I am using a IPagedList in the Razor MVC and this works perfectly. I currently have 2 search boxes (for admins) and 1 for members. Where you can search by the message and customer ID.
Now the problem is that I don't want the users to just have a textbox where you only can put in a number (for example you put in customer ID 2 and get the customer T) - but instead I want a dropdown with all the current customer names connected to the customer IDs.
I have this functionality in another page I use but it only works because I return the model on the other page and because the log page returns a "IPagedListModel" instead of a "Model" I can't use this solution. How would I make this solution work for my log page as well?
HTML code
@:<p>@Html.DropDownListFor(m => m.SelectedCustomer, Model.CustomerList)</p>
Model
using System; using System.Linq; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity; using System.Collections.Generic; using PagedList; using System.Web.Mvc; namespace ASDMVC.Models { [Table("Log")] public class LogModel { [Key] public long id { get; set; } public string message { get; set; } public DateTime timeStamp { get; set; } public string level { get; set; } public int customerId { get; set; } [NotMapped] public string Name { get; set; } } public class LogModelVM { public int SelectedCustomer { get; set; } public IEnumerable<SelectListItem> CustomerList { get; set; } public string Message { get; set; } public IPagedList<LogModel> Logs { get; set; } } public class LogDBContext:DbContext { public LogDBContext() : base("MySqlConnection") { } public DbSet <LogModel> Log { get; set; } public IQueryable<LogModel> GetLogs() { return from log in Log select log; } } }
Controller
public class DbCustomerIds { public List<DbCustomer> GetCustomerIds() { List<DbCustomer> Customers = new List<DbCustomer>(); string queryString = "SELECT * FROM dbo.customers"; SqlDataAdapter adapter = new SqlDataAdapter(queryString, System.Configuration.ConfigurationManager.ConnectionStrings["MySqlConnection"].ConnectionString); DataSet customers = new DataSet(); adapter.Fill(customers, "Customers"); foreach (DataRow item in customers.Tables[0].Rows) { DbCustomer cid = new DbCustomer(); cid.FakeId = Convert.ToInt32(item["Id"]); cid.FakeName = Convert.ToString(item["Name"]); Customers.Add(cid); } return Customers; } } private IEnumerable<SelectListItem> GetCustomerIds() { var DbCustomerIds = new DbCustomerIds(); var customers = DbCustomerIds .GetCustomerIds() .Select(x => new SelectListItem { Value = x.FakeId.ToString(), Text = x.FakeName }); return new SelectList(customers, "Value", "Text"); } } [HttpPost] public PartialViewResult LogPartialView(string searchString, string searchString2, string currentFilter, string currentFilterz, int? page, string sortOrder) { if (Roles.IsUserInRole(WebSecurity.CurrentUserName, "Admin")) { Customer = GetCustomerIds(); message = db.GetLogs(); int pageSize = 10; int pageNumber = (page ?? 1); var logs = message.OrderByDescending(i => i.timeStamp).ToPagedList(pageNumber, pageSize); foreach (var log in logs) log.Name = Customer.Where(a => a.Value == log.customerId.ToString()).FirstOrDefault().Text; LogModelVM LMVM = new LogModelVM(); LMVM.Logs = logs; LMVM.CustomerList = Customer; return PartialView("_LogPartialLayout", LMVM); } LogModelVM LMVM = new LogModelVM(); LMVM.Logs = logs; LMVM.CustomerList = Customer; return PartialView("_LogPartialLayout", LMVM); }
_LogPartialLayout
@model ASDMVC.Models.LogModelVM @using PagedList.Mvc; <link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" /> @if (Roles.IsUserInRole(WebSecurity.CurrentUserName, "Admin")) { <table class="table"> <tr> <th id="tableth"> message </th> <th id="tableth"> timestamp </th> <th id="tableth"> level </th> <th id="tableth"> customerId </th> <th id="tableth"> customerName </th> </tr> @foreach (var item in Model.Logs) { <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> } </table> } @if (Roles.IsUserInRole(WebSecurity.CurrentUserName, "Member")) { <table class="table"> <tr> <th id="tableth"> message </th> <th id="tableth"> timestamp </th> <th id="tableth"> level </th> </tr> @foreach (var item in Model.Logs) { <tr> <td> @Html.DisplayFor(modelItem => item.message) </td> <td> @Html.DisplayFor(modelItem => item.timeStamp) </td> <td> @Html.DisplayFor(modelItem => item.level) </td> </tr> } </table> } Page @(Model.Logs.PageCount < Model.Logs.PageNumber ? 0 : Model.Logs.PageNumber) of @Model.Logs.PageCount @Html.PagedListPager(Model.Logs, page => Url.Action("LogPartialView", new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, currentFilterz = ViewBag.CurrentFilterz }), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(PagedListRenderOptions.ClassicPlusFirstAndLast, new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "POST", UpdateTargetId = "divLogs" }))
Any help would be apprechiated, sorry for the long question - I just wanted to get all the information which seems relevant to the situation.
Thanks in advance.
Current error when running:
[InvalidOperationException: The model item passed into the dictionary is of type 'PagedList.PagedList`1[NowasteWebPortalMVC.Models.LogModel]', but this dictionary requires a model item of type 'NowasteWebPortalMVC.Models.LogModelVM'.]
解决方案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
andcurrentfilter
rather than usingViewBag
Side note: Ensure that all associated views, including the main view also use
@model yourAssembly.LogModelVM
这篇关于如何添加一个&QUOT;模型&QUOT;下拉列表中IPagedlist模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!