We help IT Professionals succeed at work.
Get Started

How to type cast in MVC 4?

1,806 Views
Last Modified: 2016-02-18
This is the case I'm facing:

I do have 3 controllers as following: a) MetricController b) CategoryController c) NavController

Before I go further, I better to share that I have a simple layout on View (HTML page) which consists of a column (on left) representing categories and on right portion of page I have to show a set of information. The information has to change base on selected category (report type) on left column.

MetricController is responsible to retrieve some information from a table (Metrics table) and it is something:



   public class Metric  {   //just a class for IIS log
        public int Id { get; set; }
        public string date { get; set; }
        public string time { get; set; }
        public string c_ip { get; set; }
        public string cs_username { get; set; }
        public string s_sitename { get; set; }
        public string s_computername { get; set; }
        public string s_ip { get; set; }
        AND MORE...

    }

Open in new window



 
public class MetricController : Controller {
        private IMetricRepository repository;

        public MetricController(IMetricRepository metricRepository) {
            this.repository = metricRepository;
        }

        public ViewResult List() {

            MetricsListViewModel model = new MetricsListViewModel {
                Metrics = repository.Metrics
                .OrderBy(p => p.Id)   //just few columns for now
            };

            return View(model);
        }
    }

Open in new window



NavController is responsible for navigation and showing categories (retrieved from its table) in left column as following:

public class NavController : Controller  {

        private ICategoryRepository repository;
        public NavController(ICategoryRepository repo) {
            repository = repo;
        }

        public PartialViewResult Menu() {
            IEnumerable<string> categories = repository.Categories
                                                .Where(x => x.Visible == "1" /*|| x.Visible == "0"*/)
                                                .Select(x => x.CategoryName)
                                                .Distinct()
                                                .OrderBy(x => x);

            return PartialView(categories);
        }

Open in new window


Now, I have an HTML page which shows off a left column populated with categories (or let's say report names in the left column) and need to setup and run a query every time user clicks on one of a report/category name.

One thing I know (feel free to correct me if I'm wrong) is that I have to work on CategoryController but using a code as following ends up to getting a type casting error:

public class CategoryController : Controller   {
        private IMetricRepository repository;

        public CategoryController(IMetricRepository metricRepository) {
            repository = metricRepository;
        }

        public ViewResult List(string category)
        {

            MetricsListViewModel viewModel = new MetricsListViewModel {
                Metrics = repository.Metrics
                    .Select(p => p.cs_username)  <<<<<<<<  cannot convert 
            };
        }
    }

Open in new window


The error is:

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)

This is my first MVC application and I feel like I'm missing something. I could use a constant list of report and use

@Html.ActionLink(GenerateReport1, action ,Controllername)
@Html.ActionLink(GenerateReport2, action ,Controllername)
@Html.ActionLink(GenerateReport3, action,Controllername)
but need to load the report names from a table. Any help is appreciated.

Regards,
Amit
Comment
Watch Question
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
This problem has been solved!
Unlock 1 Answer and 61 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE