Suman Bajracharya
Suman Bajracharya

Reputation: 21

ASP.NET MVC passing query result from controller to view

How to print the result of query in View page for ASP.NET MVC? My code is:

public ActionResult Index()
{
            var list = from m in db.MenuTables
                       select m.MenuName;

            return View(list);
        }

Now what should i write to print the result of this query in View Page?

Upvotes: 2

Views: 12589

Answers (4)

Pure.Krome
Pure.Krome

Reputation: 86967

Personally, I would get in the habit of having ViewModels and then strongly typing your View, to that model.

The model will expose ONLY THE DATA you want to display. Nothing more, nothing less. So let's assume you want to display the Name, Price and some other meta data.

Pseudo-code:

//View Model
public class MenuItem
{
    public string Name { get; set; }
    public decimal Price { get; set; }
    public bool IsVegetarian { get; set; ]
}

public class IndexViewModel
{
    public IList<MenuItem> MenuItems { get; set; }
    public string MaybeSomeMessage { get; set; }
}

//in Controller
public ActionResult Index()
{
    // This gets the menu items from your db, or cache or whatever.
    var menuItemsFromDb = GetMenuItems(); 


    // Let's start populating the view model.
    IndexViewModel model = new IndexViewModel();    

    // Project the results to your model.
    IList<MenuItems> menuItems = null;
    if (menuItemsFromDb != null)
    {
        model.MenuItems = (from menuItem in menuItemsFromDb
                           select new MenuItem() {
                               Name = menuItem.Name,
                               Price = menuItem.Price,
                               IsVegetarian = menuItem.IsVegetarian
                           }).ToList();
    }
  
    // Anything else...
    model.MaybeSomeMessage = "Hi There!";

    return View(model);
}

//in View
@model IndexViewModel

<h3>@Model.MaybeSomeMessage</h3>
<ul>
    @foreach(var item in Model.MenuItems)
    {
        <li><a href="#">@item.Name</a> - $ @item.Price</li>
    }
</ul>

etc..

Note I've skipped some error checking, etc.

The point: only pass what you need.

At first, you may think this is much more code than is necessary. The best answer I can suggest to that thought, is that in the long run, you'll thank yourself for getting in the habit of this because the view should only ever know about the exact data it requires.

Nothing more, nothing less. Sending the least amount of data means you have a very light and simple view which will make your support/debugging much better. Next, you'll be able to unit test your controllers with a lot more intelligence and smarts, when you get to that.

Upvotes: 4

Hamid N K
Hamid N K

Reputation: 87

     public ActionResult Index()
    {
        var list = from m in db.MenuTables
                   select m.MenuName;

        return View(list);
    }

    //In View

    @model IEnumerable<ProjectName.models.MenuTables>

    @foreach(var item in Model)
    { 
        @item.Field_Name
    }

Upvotes: 0

Khalid Abuhakmeh
Khalid Abuhakmeh

Reputation: 10839

The first thing you want to do is call ToList() or else you could possibly be executing the same SQL query multiple times.

   public ActionResult Index()
   { 
        var list = (from m in db.MenuTables
                   select m.MenuName).ToList();

        return View(list);
    }

Secondly, I wouldn't just pass up a full list like that. You should create a ViewModel. That will allow you to pass up more data later on with a smaller effort.

     public ActionResult Index()
     {
        var model = new IndexModel();
        model.Tables = db.MenuTables.ToList();
        model.AnotherValue = "MENUS";

        return View(model);
     }

Now we are on the view, you will need to set the model and iterate the table.

 @model IndexModel

 <h3>@Model.AnotherValue</h3>
 <ul>
 @foreach( var table in Model.Tables) {
     <li>@table.Name<li>
 }
 </ul>

Upvotes: 0

Travis J
Travis J

Reputation: 82297

Assuming that list is an IEnumerable of strings (i.e. that MenuName is a string).

In your view, accept the model IEnumerable<string>

@model IEnumerable<string>

and then enumerate it

@foreach( string s in Model )
{
 <div>
  @s
 </div>
}

Upvotes: 1

Related Questions