0

I am trying to implement a method where I select all records that belong to the correct StateId (NJ, DE, PA); I am using dependency injection and the repository design pattern which I am new to.

Here is what I have in my controller:

private IWorkOrderRepository _workOrderRepository;

public DependencyInjectionController(IWorkOrderRepository workOrderRepository)
{
    _workOrderRepository = workOrderRepository;
}

public IActionResult WorkOrders()
{
    var model = _workOrderRepository.GetWorkOrders();
    return View(model);
}

public IActionResult Nj(string LocationFilter)
{
    var locations = _workOrderRepository.GetWorkOrders().Select(x => x.StateId == LocationFilter).ToList();
    return View(locations);
}

The GetWorkOrders method works exactly as it's supposed to. Here is my Model:

public class WorkOrderContext : DbContext
{
    public DbSet<WorkOrder> WorkOrder { get; set; }

    public WorkOrderContext(DbContextOptions<WorkOrderContext> options)
      : base(options)
    {

    }

}

public class WorkOrder
{

    [Key]
    public int UserId { get; set; }

    public string LocationId { get; set; }

    public string Reason { get; set; }

    public bool IsActive { get; set; } = true;

    public DateTime Date { get; set; } = DateTime.Now;

    public string StateId { get; set; } 

} 

The error I am getting when I hit the route is:

"The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List1[System.Boolean]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable1[WorkOrderServices.Models.WorkOrder]'."

1 Answer 1

4

Select is transforming the list of work orders to booleans: true where the StateId matches the location filter, and false where it doesn't.

Instead of transforming the list, you want to filter it with Where: Where(x => x.StateId == LocationFilter).

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. I am no longer getting the error but it is not returning any records. I assume because I don't have anything passing the specific StateId through. Do you know how I could accomplish that? Thanks
Try putting a debugger breakpoint in Nj to make sure you're actually using the value for LocationFilter that you think you are. Also try leaving off the Where and just returning the whole set of WorkOrders as an experiment. Make sure there's actually data in the database as well. If all that fails, you'll probably want to open another question.
Hey thanks so its working now because I made linq query: Where(x => x.StateId == "NJ") I just wish that I could do it so it could be dynamically passed in as a parameter
Would something this work, then pass through "Nj" into the function in the IactionResult: ```` public GetByStateId(StateId LocationFilter) { var list = _workOrderRepository.GetWorkOrders().Where(x => x.StateId == LocationFilter).ToList(); return list(LocationFilter); }````
You should open a separate question for this, with some more detail about the scenario you're trying to implement, particularly regarding where you want the desired filter value to come from (i.e. a view where the user selects it? A querystring parameter in the URL? Configuration?).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.