0

It is said that the models should be fat and the Views should be thin.We put our business logic inside the Model (https://stackoverflow.com/questions/235233/asp-net-mvc-should-business-logic-exist-in-controllers).We normally write the LINQ inside the controller,but is it possible that we should write the query in models,If yes then how we will get the results in the View?

Second Question

  public ActionResult Index()
  {
            using (NORTHWNDEntities c = new NORTHWNDEntities())
            {

                var x = c.Employees.Count();
                ViewData["count"] = x;

                return View(x);       
            }
    }

When we do this are we passing the variable x to the View? I tried to access the ViewData in View

<% ViewData["count"] %>

But it gives an error error ,Anyone who can help me with this Thanks

Community
  • 1
  • 1
require_once
  • 1,995
  • 3
  • 21
  • 29
  • 2
    Business logic should go in the controller, not the model. – Jeremy Holovacs Jun 01 '12 at 17:46
  • @JeremyHolovacs If I want to put it in Model then?it is not possible? – require_once Jun 01 '12 at 17:49
  • 2
    doing so violates the MVC pattern. The models are supposed to be "dumb", just holders of data. The controllers put data in them and use data from them (business logic). The view, too, should be fairly dumb; the only logic they should have is what is necessary to render the data in the model. – Jeremy Holovacs Jun 01 '12 at 17:51
  • @JeremyHolovacs So you mean that we can't use LINQ in Models? – require_once Jun 01 '12 at 17:53
  • 1
    you can do whatever you like. it is flexible. however, it is best to separate your business logic. – c0deNinja Jun 01 '12 at 17:53
  • @Mentor, "can't" is a strong word... but for all intents and purposes, LINQ is used in data manipulation, and that shouldn't be happening in the model. The model should just hold data. – Jeremy Holovacs Jun 01 '12 at 17:55
  • 1
    @Mentor There is a reason everyone is telling you to put LINQ in your business layer. I don't know why you are trying to put LINQ in your model, but you're most likely creating bad code. – Allensb Jun 01 '12 at 18:05
  • @Allensb To be honest I was asked in the interview to do this – require_once Jun 01 '12 at 19:02
  • @JeremyHolovacs Logic should be in Controller http://stackoverflow.com/questions/235233/asp-net-mvc-should-business-logic-exist-in-controllers – require_once Jun 01 '12 at 19:06
  • In that case I'm pretty sure that business logic is referencing rules and validation principals, which I do not consider "logic" as much as "rules". When I refer to business logic I am referring to interfacing with the persistence layer, and storing/ using the data for business purposes. Attempting to do that in the model would be horrific. – Jeremy Holovacs Jun 01 '12 at 19:20

3 Answers3

2

If you are trying to display the value of ViewData["count"] in your view, you can use the following syntax:

<%= ViewData["count"] %>

Note the = in the opening tag. This is the equivalent of

<% Response.Write(ViewData["count"]) %>
jrummell
  • 42,637
  • 17
  • 112
  • 171
2

there is better approach for doing this.and is very straight forward.create a Model that meets your needs and pass it to view.

public class MyModel
{
    public int Count{get;set;}
}

and your controller can looks like

public ActionResult Index()
  {
            using (NORTHWNDEntities c = new NORTHWNDEntities())
            {

                var x = c.Employees.Count();
                var model = new MyModel{Count = x};    
                return View(model);       
            }
    }

and then create an strongly typed view

Razor Syntax :

@model MyModel

@Model.Count

ASPX syntax :

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Models.MyModel>" %>
<%= Model.Count %>

and the code can get even better if you do the following:

public class EmployeeService
{

     public int GetEmployeeCount()
     {
            using (NORTHWNDEntities c = new NORTHWNDEntities())
            {
                var count = c.Employees.Count();                   
                return count;       
            }
     }
}

and the controller most change as well:

public ActionResult Index()
  { 
                EmployeeService srvc = new EmployeeService();
                var x = srvc.GetEmployeeCount();
                var model = new MyModel{Count = x};    
                return View(model);   
  }
Behnam Esmaili
  • 5,835
  • 6
  • 32
  • 63
0

The query have to be into the Data Access Layer and the logic in MVC is into the Controller, not into the Model.

Here you can find an example of a layered architecture with MVC.

At the end you have always to use a Model into the View, don't pass data using the ViewData.

Community
  • 1
  • 1
Matteo Migliore
  • 925
  • 8
  • 22