【发布时间】:2015-10-07 16:16:18
【问题描述】:
我有一个页面可以在 html 表中显示每个日志(消息、时间、类型、customerId、名称)。由于日志很大,我在 Razor MVC 中使用了 IPagedList,这非常有效。我目前有 2 个搜索框(用于管理员)和 1 个用于成员。您可以在哪里通过消息和客户 ID 进行搜索。
现在的问题是我不希望用户只有一个文本框,您只能在其中输入一个数字(例如,您输入客户 ID 2 并获取客户 T) - 但我想要一个下拉列表将所有当前客户名称与客户 ID 关联。
我在我使用的另一个页面中具有此功能,但它仅起作用,因为我在另一个页面上返回模型,并且因为日志页面返回“IPagedListModel”而不是“模型”,所以我无法使用此解决方案。我如何让这个解决方案也适用于我的日志页面?
HTML 代码
@:<p>@Html.DropDownListFor(m => m.SelectedCustomer, Model.CustomerList)</p>
型号
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;
}
}
}
控制器
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"
}))
任何帮助都将不胜感激,对于这个冗长的问题,我很抱歉 - 我只是想获得所有似乎与情况相关的信息。
提前致谢。
运行时的当前错误:
[InvalidOperationException:传入字典的模型项的类型为“PagedList.PagedList`1[NowasteWebPortalMVC.Models.LogModel]”,但此字典需要“NowasteWebPortalMVC.Models.LogModelVM”类型的模型项。]
【问题讨论】:
-
使用具有
SelectedCustomerId、CustomerIDItem和IPagedList集合属性的视图模型 -
我该怎么做? @StephenMuecke
-
您能否展示创建
PagedListPager的视图,以便我确定您在做什么 -
另请注意
var customers是IEnumerable<SelectListItem>因此转换为另一个IEnumerable<SelectListItem>(这就是SelectList是什么)毫无意义的额外开销所以它应该只是return customers; -
@StephenMuecke 添加了视图。第二部分将在主要问题解决后进行更改,因为该部分现在可以正常工作。
标签: c# asp.net-mvc asp.net-mvc-4 razor