Asp.net MVC模型示例正确或不正确

编程入门 行业动态 更新时间:2024-10-26 10:33:23
本文介绍了Asp MVC模型示例正确或不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有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模型示例正确或不正确

本文发布于:2023-11-12 09:35:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581128.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   示例   或不   模型   Asp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!