1

I m wondering a MVC model class may have functions in them or not ? I want to use these methods in View to accept an argument and do some business logic.

Is it standard or a bad idea ? Or is any issue associated with this approach ?

user576510
  • 5,777
  • 20
  • 81
  • 144
  • 1
    This is the site where you can have idea about what is MVC and how it works. http://www.asp.net/mvc – Dhwani Oct 16 '14 at 04:45
  • 4
    Why are people down voting this question? Maybe the question is our of place and should be in http://programmers.stackexchange.com/. But the question is a valid question and the OP is asking for something developers go through in their lifetime!! – Abdel Raoof Olakara Oct 16 '14 at 05:01
  • 1
    My answer to a similar question: http://stackoverflow.com/a/14132184/453277. In brief, I like to keep models and controllers very simple and put (potentially reusable) logic into other classes. – Tim M. Oct 16 '14 at 05:29

2 Answers2

5

In my eyes no, its not standard unless if in your mind Models are not just simple classes...

Try this:

Keep your models separated:

-Models that serve your business layer and your data access layer. Lets call them just Models.

-Models that serve your MVC views. Lets call them ViewModels.

Then try to see your controllers as traffic police officers. Let them just take the request and assign someone to do the actual job.

This means that its better to create 2 separate parts in your application.

-The business Layer. -The Data access Layer.

If the app is small you could make the 2 above layers one.

So after all that you will have:

-Controllers talk to Views with ViewModels. -Controllers talk to Business / Data access layer using Models.

So keep your models and viewmodels thin and do the business logic NOT in the controller but in separate layers (it could be another project or just another class in your project).

Additional Info:

You could have your models like:

public class Course
{
    public int CourseId { get; set; }
    public string CourseName { get; set; }
}

public class Faculty
{
    public int FacultyId { get; set; }
    public string FacultyName { get; set; }
    public List<Course> AllotedCourses { get; set; }
}

public class Student
{
    public int EnrollmentNo { get; set; }
    public string StudentName { get; set; }
    public List<Course> EnrolledCourses { get; set; }
}

Your ViewModel:

public class ViewModelDemoVM
{        
    public List <Course> allCourses { get; set; }
    public List <Student> allStudents { get; set; }
    public List <Faculty> allFaculties { get; set; }
}

Then create a separate class that will handle the models and return viewmodels for controllers to pass to views and vice versa.

e4rthdog
  • 5,103
  • 4
  • 40
  • 89
  • its like that, ViewModels are composed of Models and Models has some AutoMapIgnored / NotMapped fields. So method needs to be a part of model not viewmodels. Now how it is to put functions / methods in Models ? – user576510 Oct 16 '14 at 04:52
  • I just say dont put them in model classes..Create a separate class. then in your controllers , create a new object of that class and use that classes methods...that class should access model instances – e4rthdog Oct 16 '14 at 04:54
  • 1
    +1, but 4 things. ... A) a more complete example on how it should be, will be better (than how not to be). I mean, one very small example with a model, modelview, controller, view will be best. ... B) Looks like, u meant to say model is only setter/getter methods, that means u can assign value and take out value. ... C). not sure if constructor is allowed to set all these values at once. ... D) theory/ explanation in point wise is always better – Manohar Reddy Poreddy Nov 30 '18 at 07:37
1

I usually put all business logic inside the model classes and try to make the controllers as stupid as possible.

Personally, I think you shouldn't put any business logic in view unless there's no other way around.

May be you could show us your code and it's easier to discuss from there.

Toby D
  • 1,465
  • 1
  • 19
  • 31
  • thanks, by saying "I put all busienss logic inside model classes" are you saying it is good to keep functions in Models classes ? What other way you are using to put busienss logic in Model class ? – user576510 Oct 16 '14 at 04:54