mattbloke
mattbloke

Reputation: 177

method within model in MVC

I have a model in my MVC app, 'designation'. I have a method getdesig() to simply return all of the designations. I originally had this in my controller but moved to my model recently, with the aim of decluttering my controller and making it thinner. Is this model a logical place to put such a method?

public class designation
{
    [Key]
    public int DesignationID { get; set; }
    public string DesignationName { get; set; }

    public virtual ICollection<user> users { get; set; }


    private ClaimContext db = new ClaimContext();
    public List<designation> getdesig()
    {

        {
            return (from c in db.designations
                    select c).ToList();
        }
    }

}

Upvotes: 0

Views: 49

Answers (1)

T McKeown
T McKeown

Reputation: 12847

yes, however in more complicated scenarios where conditions exists the controller is the spot to determine "What" needs to be loaded or "how much" needs to be loaded based off the scenario/arguments. In this very simple example it is fine as you are just dumping all the data without regard to any context.

It is a good practice to keep your ViewModels as simple and specific to the View as possible, the ViewModel's job is to simply be a common store to drive the view. The view relies on the model to be set with the appropriate data, it is the job of the controller to determine context and what should be populated in the model.

Upvotes: 1

Related Questions