Asp.Net Core 入门(四)—— Model、View、Controller

  和我们学习Asp.Net MVC一样,Asp.Net Core MVC的Model、View、Controller也和我们熟悉的Asp.Net MVC中的相似。不同的是我们在使用Asp.Net Core MVC的时候需要注入MVC。

  Asp.Net Core MVC注入 MVC 的方法有两种,一种是AddMvcCore(),它只是注入了MVC的一些核心的东西,不包括像返回的ViewResult、JsonResult等,如果要使用完整的MVC,需要使用另一种注入方式 AddMvc() 进行注入,AddMvc()里调用了AddMvcCore()。

  下面我们就来详细说说模型、视图、控制器。

  我们新建完一个MVC项目后,模板帮我们生成了Models、Views、Controllers三个文件夹,一般我们把新建的Model放在Models中,但实际项目上我们通常会新建一个项目放model。

  MVC 中的模型包含一组表示数据的类和管理该数据的逻辑。View和Controller之间可以通过Model来传递数据。 我们定义一个学生类,一个学生仓储接口还有一个该接口的实现。

    /// <summary>
    /// 学生模型
    /// </summary>
    public class Student
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string ClassName { get; set; }

        public string Email { get; set; }
    }
  public interface IStudentRepository
    {
        Student GetStudent(int id);
    }
 public class MockStudentRepository : IStudentRepository
    {
        private List<Student> _studentList;
        public MockStudentRepository()
        {
            _studentList = new List<Student>();
            _studentList.AddRange(new List<Student>() {
                new Student(){Id=1,Name="张三",ClassName="三年一班",Email="[email protected]"},
                new Student(){Id=2,Name="李四",ClassName="三年一班",Email="[email protected]"},
                new Student(){Id=3,Name="王五",ClassName="三年一班",Email="[email protected]"},
                new Student(){Id=4,Name="赵六",ClassName="三年一班",Email="[email protected]"},
                new Student(){Id=5,Name="钱七",ClassName="三年一班",Email="[email protected]"},
            });
        }


        public Student GetStudent(int id)
        {
            return _studentList.Where(s => s.Id == id).FirstOrDefault();
        }
    }

  新建一个HomeController,添加代码

      private readonly IStudentRepository _studentRepository;
        //构造函数注入
        public HomeController(IStudentRepository studentRepository)
        {
            _studentRepository = studentRepository;
        }

        public IActionResult Index(int id)
        {
            return View(_studentRepository.GetStudent(id));
        }

  添加视图Index.cshtml

@model StudentManagement.Models.Student
@{
    ViewData["Title"] = "Index";
}

<div>
    @Model.Id
    @Model.Name
    @Model.ClassName
    @Model.Email
</div>

  我们可以看到,Controller通过Repository获取到Student的数据后将其作为View的参数返回了,而在视图Index我们可以使用@model StudentManagement.Models.Student来引用模型。

  当然,将数据传递到视图还有其他的方式,这一点和我们在Asp.Net MVC的时候是一样的,我们可以总结一下:

  1、ViewData:弱类型的字典对象,使用string类型的键值对,运行时动态解析,没有智能感知,编译也没有检查类型。

  2、ViewBag:ViewData的包装器,都创建了一个弱类型的视图,使用动态属性来存储和查询数据。

  3、强类型视图:在视图中使用@model指令指定模型类型,使用@Model访问模型对象属性,提供编译类型检查和智能提示。

  需要注意的是,在我们HomeController的构造函数里,形参 IIStudentRepository 是怎么传入的呢?我们知道Asp.Net Core自带了依赖注入容器,在前面讲Startup文件时候知道ConfigureServices方法是用来注入服务的,所以我们需要将我们的 IIStudentRepository 注入进去,否则运行会抛异常。

services.AddTransient<IStudentRepository, MockStudentRepository>();

  在返回VIew的时候,上面例子我们传了一个model作为参数,实际上它也可以传递视图名称,在传递视图名称的时候可以使用绝对路径或相对路径,绝对路径必须指定.cshtml文件扩展名,相对路径不用带扩展名.cshtml。

   我们定义一个Detail视图,然后调用return View("Detail") 

  public ObjectResult Detail()
  {
      return new ObjectResult(_studentRepository.GetStudent(1));
  }
 return View("Detail"); 

  我们使用的是相对路径,那么.Net Core会执行下面的操作去查找视图

  1、在“/ Views / Home /”文件夹中查找,找到就返回,找不到继续往第2步找。

  2、在“/ Views / Shared /”文件夹中查找,找到就返回,找不到继续往第3步找。

  3、在“/ Pages / Shared /”文件夹中查找, 如果找到视图文件,则视图生成的 HTML 将发送回发出请求的客户端。如果找不到视图文件,将收到错误。

    The view 'Detail' was not found  
  
  最后,讲一下ViewModel模型视图的一个概念,很多时候我们的视图需要的数据往往比我们定义的Model能提供的数据要多,这时候就可以使用ViewModel了,在实际项目开发中,ViewModel也等于DTO(数据传输对象)。至于ViewModel的使用和Model一样,传递到View中进行显示。

猜你喜欢

转载自www.cnblogs.com/jesen1315/p/11041688.html
今日推荐