Reputation: 5755
I'm very new to ASP.Net MVC but very familiar with the ASP.Net classic. I've been trying to learn the MVC pattern and I've come across this question. Let's say I have a database and I'd like to show some data from a table automatically in the home page. So far I have created a Model called the DemoModel, but I'm handling this task this way:
// This is not a contoller
class ShowData {
public static DataContext DC = new DataContext ("My Connection String");
public static IEnumerable<DemoModel> GetData () {
var Query = DC.GetTable<DemoModel>().ToList();
for (int i = 0; i < 4; i++) {
yield return Query[i];
}
}
and then using the Razor
@{
var Query = ShowData.GetData().OrderBy(x=> x.Date);
foreach (var i in Query) {
<div> i.First </div>
<div>i.Last </div>
......
}
}
My question is related to the MVC pattern, of course I can achive what I want this way, but is it the right way to display data in MVC or do I have to do everything inside the appropriate conrtroller ? if so How can I return what I want in the controller ?
Upvotes: 0
Views: 81
Reputation: 1281
Well what you are doing isn't wrong. Could it be cleaner? Sure. For instance adding a for loop in your view is not favored. You can try perhaps binding your model to an @Html.Listbox
Upvotes: 1