0
votes

I have asp.net MVC application.

And I have a main page (View).

The data in the view contains: news headlines, weather update, sports section.

So for this I have created a model.

Model contains all the requirements: news headlines (list of class type News), weather (string weather), sports section (list of class SportSection)

So my model class is something like this

public class Main
{
     public List<News> news=new List<News>();
     public List<SportSection> results=new List<SportSection>()
     public string weather;
}

And I am populating my data in Controller. And sending my model to view in Controller's action method like this

 Models.Main m=new Models.Main()
 ...
 ...
 ...

 return View(m);

I need to know, my understanding of Asp.net MVC Models is correct as it is shown in example above?

1
Depends, what you mean by correct? - adricadar
I meant, according to the example in the question I need to know that my use of Model is not breaking any norms of MVC architecture? It's not outlandish in any way - user786

1 Answers

1
votes

Your Model respect the purpose for keeping data for transport and don't have behaviour.

Instead of fields you have to use properties and you can respect naming convention from C#.

public class Main
{
     public List<News> News { get; set; }
     public List<SportSection> Results { get; set; }
     public string Weather { get; set; }

    public Main()
    {
        News=new List<News>();
        Results=new List<SportSection>()
    }
}

Note: Also there is a ViewModel and the role of him is to keep only relevant data, so if you don't need some properties you need to remove them (i.e. if the Weather is not needed in your View, remove the Weather property)