我有一个页面可以在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:传递到字典中的模型项的类型为&#39; PagedList.PagedList`1 [NowasteWebPortalMVC.Models.LogModel]&#39;,但此字典需要类型为&#39;的模型项。 NowasteWebPortalMVC.Models.LogModelVM&#39;。]