Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8921285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:30:06+00:00 2026-06-15T06:30:06+00:00

I have a model: public partial class VisitorLog { public int Id { get;

  • 0

I have a model:

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; }
}

a generic repository:

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();
    }
}

an interface for the generic repository:

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();
}

my model-specific repository (which inherits the generic):

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; }
}

my controller that is loading a viewmodel into the view:

    var searchViewModel = new SearchViewModel
{
    VisitorLogs = iVisitorlogRepository.FindBy(v => v.VisitorName.Contains(searchText)
        || v.CompanyName.Contains(searchText)
        || v.EmployeeID.Contains(searchText)),
};

and finally my view:

@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>

Everything is working great with one exception. The view shows a list of Visitors (VisitorLog). Using navigation properties I was able to get the EmployeeName value to show instead of the employee ID from the VisitorLog class by simply using @item.Employees.EmployeeName. However, I also want to be able to search by employee name in my repository call in the controller:

VisitorLogs = iVisitorlogRepository.FindBy(v => v.VisitorName.Contains(searchText)
|| v.CompanyName.Contains(searchText)
|| v.Employees.EmployeeName.Contains(searchText)),

but Employees.EmployeeName in the example above is not showing as an option in the list members for “v”.

Any help is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:30:08+00:00Added an answer on June 15, 2026 at 6:30 am

    Looks like this might just be a typo – the navigation property in your class is called Employee, not Employees. Does the following work?

    VisitorLogs = iVisitorlogRepository.FindBy(
        v => v.VisitorName.Contains(searchText)
        || v.CompanyName.Contains(searchText)
        || v.Employee.EmployeeName.Contains(searchText)),
    

    If this was the reason, you’ve done the same thing in the View.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following model: public class Tag { public int Id { get;
I have the following Model: public class DeliveryTracking { public string TrackingRef { get;
Assume i have this model public partial class Todo { public int id {
I have a class public partial class Advertisement { public int AdvertisementId { get;
So I have the following model: public class Person { public String FirstName {
I have a partial class in a dbml file. public partial class Comment string
For example I have two entities Class A { public Guid Id {get;set;} public
I have a model with complex properties like: public partial class CrmDefinedEntity { private
I have Entity set and i create partial class [Bind(Include = ID,Note,Spec,Retired)] [MetadataType(typeof(SomeClass))] public
I have model public class UserModel : IUserModel { public LocationModel WorkLocation { get;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.