我有一个模特:
public partial class VisitorLog
{
public int Id { get; set; }
public string VisitorName { get; set; }
public DateTime TimeIn { get; set; }
public DateTime TimeOut { get; set; }
public string CompanyName { get; set; }
public string EmployeeID { get; set; }
public string VisitReason { get; set; }
// Navigation properties
[ForeignKey("EmployeeID")]
public virtual Employee Employee { get; set; }
}
通用存储库:
public class GenericRepository<C, T> : IGenericRepository<T> where T : class where C : DbContext, new()
{
private C _entities = new C();
public C Context
{
get { return _entities; }
set { _entities = value; }
}
public virtual IQueryable<T> GetAll()
{
IQueryable<T> query = _entities.Set<T>();
return query;
}
public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
{
IQueryable<T> query = _entities.Set<T>().Where(predicate);
return query;
}
public virtual void Add(T entity)
{
_entities.Set<T>().Add(entity);
}
public virtual void Delete(T entity)
{
_entities.Entry(entity).State = System.Data.EntityState.Deleted;
}
public virtual void Edit(T entity)
{
_entities.Entry(entity).State = System.Data.EntityState.Modified;
}
public virtual void Save()
{
_entities.SaveChanges();
}
}
通用存储库的接口:
public interface IGenericRepository<T> where T : class
{
IQueryable<T> GetAll();
IQueryable<T> FindBy(Expression<Func<T, bool>> predicate);
void Add(T entity);
void Delete(T entity);
void Edit(T entity);
void Save();
}
我的特定于模型的存储库(继承了泛型):
public class VisitorLogRepository : GenericRepository<VisitorLogDBContext, VisitorLog>, IVisitorLogRepository {
public VisitorLog GetSingle(int id)
{
var query = GetAll().FirstOrDefault(x => x.Id == id);
return query;
}
}
public class SearchViewModel
{
public IEnumerable<VisitorLog> VisitorLogs { get; set; }
}
我的控制器正在将视图模型加载到视图中:
var searchViewModel = new SearchViewModel
{
VisitorLogs = iVisitorlogRepository.FindBy(v => v.VisitorName.Contains(searchText)
|| v.CompanyName.Contains(searchText)
|| v.EmployeeID.Contains(searchText)),
};
最后我的观点:
@model VisitorLogApp.ViewModels.SearchViewModel
@foreach (var item in ViewData.Model.VisitorLogs) {
<tr>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
<td>
@item.VisitorName
</td>
<td>
@item.CompanyName
</td>
<td>
@item.Employees.EmployeeName
</td>
<td>
@String.Format("{0:g}", item.TimeIn)
</td>
<td>
@String.Format("{0:g}", item.TimeOut)
</td>
<td>
@item.VisitReason
</td>
</tr>
除了一个例外,一切都很好。该视图显示了访问者列表(VisitorLog)。使用导航属性我只需使用 @ item.Employees.EmployeeName 即可获取EmployLogName值而不是来自VisitorLog类的员工ID。但是,我还希望能够在控制器的存储库调用中按员工姓名进行搜索:
VisitorLogs = iVisitorlogRepository.FindBy(v => v.VisitorName.Contains(searchText)
|| v.CompanyName.Contains(searchText)
|| v.Employees.EmployeeName.Contains(searchText)),
但上面示例中的 Employees.EmployeeName 未在&#34; v&#34;的列表成员中显示为选项。
感谢任何帮助。
答案 0 :(得分:1)
看起来这可能只是一个错字 - 您班级中的导航属性称为Employee
,而不是Employees
。以下是否有效?
VisitorLogs = iVisitorlogRepository.FindBy(
v => v.VisitorName.Contains(searchText)
|| v.CompanyName.Contains(searchText)
|| v.Employee.EmployeeName.Contains(searchText)),
如果这是原因,你在视图中做了同样的事情。