0

I am simply trying to retrieve some data from a database, and display it on the _Layouts.cshtml file. And i wanna do this in the with a method in the models folder, right??

My following script down here works, but it's not pretty, take a look.

Here is my model:

namespace MvcBreakingNews.Models
{
public class ListCategories
    {
        public IList<string> arrTest() {
            IList<string> myList = new List<string>();
            DataClassesDataContext dt = new DataClassesDataContext();

            var q = from c in dt.categories select c;
            foreach (var item in q)
            {
                myList.Add(item.category1.ToString());
            }
                return myList;
            }
        }
    }
}

And here is my _Layout.cshtml

@using MvcBreakingNews.Models
...

@{
   ListCategories objC = new ListCategories();

   foreach (var item in objC.arrTest())
   {
       <li><a href="#">@item</a></li>         
   }
}

Now what i want to do, or what i think i want to do. Is to get rid of the foreach loop in my method and send the array directly to the _Layout.cshtml

How would i do this?

tereško
  • 58,060
  • 25
  • 98
  • 150
BjarkeCK
  • 5,694
  • 5
  • 41
  • 59

3 Answers3

1

You can use the ToList() extension method:

public IList<string> arrTest()
{
    DataClassesDataContext dt = new DataClassesDataContext();

    var q = from c in dt.categories
            select c.category1.ToString();

    return q.ToList();
}

Or you could return q directly, which would require changing the return type:

public IQueryable<string> arrTest()
{
    DataClassesDataContext dt = new DataClassesDataContext();

    return from c in dt.categories
           select c.category1.ToString();
}
svick
  • 236,525
  • 50
  • 385
  • 514
  • Thank you, but they did not work for me. Both gave me the same error: "You cannot implicitly convert type 'blah blah' Are you missing a cast? But a solution in that direction would be great. – BjarkeCK Oct 01 '11 at 14:54
  • Sorry, I overlooked the code you had inside of the `foreach`. See updated answer. – svick Oct 01 '11 at 15:00
  • Cool this works! Thank you very much! Last question, what do i do if i want to select multiple table cells? – BjarkeCK Oct 01 '11 at 15:36
  • You mean multiple columns? You can return the whole category object from the `arrTest` method and access that in your cshtml file. – svick Oct 01 '11 at 15:40
  • Ah yes! Columns, sorry. Hope you don't mind me asking! But how would that look :)? ` ------ public DataClassesDataContext arrrTest() { DataClassesDataContext dt = new DataClassesDataContext(); return dt; } ---------layout:----- ListCategories objC = new ListCategories(); var q = from c in objC.arrrTest().categories select c; ----- ` Witch does not work :( – BjarkeCK Oct 01 '11 at 15:59
0

Best way of achieving this is to create a BaseController and makes all of your controllers derived from that BaseController.

What your new BaseController does is to pass a model for every single time. I hope you got the idea.

Here is a good example :

How to create a strongly typed master page using a base controller in ASP.NET MVC

Community
  • 1
  • 1
tugberk
  • 57,477
  • 67
  • 243
  • 335
  • Auch. Trying to wrap my head around the idea and the example. I think ill go for i simpler solution to begin with, but i will get back to this! Thanks. – BjarkeCK Oct 01 '11 at 14:59
  • @user973485 your solution is so fine to get things done quickly but it is not supporting the idea of **Separation of Concerns**. Your views shouldn't care where the data is coming from. It should only care about how to format and output it. – tugberk Oct 01 '11 at 15:06
0

The shortest way to do the same using Lembda expression is as follows.

public IList<string> arrTest()
{
    DataClassesDataContext dt = new DataClassesDataContext();

    return dt.categories.select(c=>c.category1.ToString()).ToList();
}
Abdul Qadir Memon
  • 950
  • 1
  • 12
  • 27