我有asp MVC应用程序.
I have asp 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.
模型包含所有要求:新闻标题(新闻类类别的列表),天气(字符串天气),体育部分(SportSection类的清单)
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; }然后我将数据填充到Controller中.并发送我的模型以在Controller的这种操作方法中查看
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);我需要知道,我对Asp MVC模型的理解是正确的,如上面的示例所示?
I need to know, my understanding of Asp MVC Models is correct as it is shown in example above?
推荐答案您的模型尊重保留数据进行传输的目的,并且没有任何行为.
Your Model respect the purpose for keeping data for transport and don't have behaviour.
您必须使用 properties 而不是 fields ,并且可以遵循C#中的命名约定.
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>() } }注意:还有一个 ViewModel ,他的作用是仅保留相关数据,因此,如果不需要某些属性,则需要删除它们(即,如果视图中不需要 Weather ,请删除 Weather 属性)
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)
更多推荐
Asp.net MVC模型示例正确或不正确
发布评论