ASP.NET MVC - 数据模型

ASP.NET MVC - 数据模型 首页 / ASP.Net MVC入门教程 / ASP.NET MVC - 数据模型

在本章中,我们将讨论在ASP.NET MVC Framework应用程序中构建模型的问题,模型存储根据控制器中的命令检索并显示在视图中的数据。

模型是类的集合,您将在其中使用数据和业务逻辑,因此,基本上,模型是特定于业务领域的容器,它用于与数据库进行交互,它还可以用于操纵数据以实现业务逻辑。

通过创建一个新的ASP.Net MVC项目,让我们看一下View的简单示例。

步骤1 - 打开Visual Studio。单击File→New→Item菜单选项。

将打开一个"New Project"对话框。

Open the Visual Studio

步骤2 - 在左侧窗格中,选择Template→Visual C#→Web。

步骤3 - 在中间窗格中,选择ASP.NET Web应用程序。

步骤4 - 在"Name"字段中输入项目名称" MVCSimpleApp",然后单击"OK"继续。您将看到以下对话框,要求您设置ASP.NET项目的初始内容。

MVCSimpleApp

步骤5 - 为了简化操作,请选择"Empty"选项,然后在"Add folders and core references for"部分中选中" MVC"复选框,然后单击"OK"。

我们现在需要添加一个控制器。

步骤6 - 右键单击Solution Explorer中的controller文件夹,然后选择Add→Controller。

无涯教程网

它将显示"Add Scaffold"对话框。

Right-click Controller Folder

步骤7 - 选择MVC 5 Controller-带有read/write操作选项,该模板将使用Controller的默认操作创建一个Index方法,这还将列出其他方法,如"Edit/Delete/Create"。

步骤8 - 单击"Add"按钮,将出现"Add Controller"对话框。

Add Employee Controller

步骤9 - 将名称设置为EmployeeController,然后单击"Add"按钮。

步骤10 - 您将在Controllers文件夹中看到一个新的C#文件" EmployeeController.cs",该文件已打开,可以在Visual Studio中使用一些默认操作进行编辑。

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;

namespace MVCSimpleApp.Controllers {
   public class EmployeeController : Controller{
      //GET: Employee
      public ActionResult Index(){
         return View();
      }
		
      //GET: Employee/Details/5
      public ActionResult Details(int id){
         return View();
      }
		
      //GET: Employee/Create
      public ActionResult Create(){
         return View();
      }
		
      //POST: Employee/Create
      [HttpPost]
      public ActionResult Create(FormCollection collection){
         try{
            //TODO: Add insert logic here
            return RedirectToAction("Index");
         }catch{
            return View();
         }
      }
		
      //GET: Employee/Edit/5
      public ActionResult Edit(int id){
         return View();
      }
		
      //POST: Employee/Edit/5
      [HttpPost]
      public ActionResult Edit(int id, FormCollection collection){
         try{
            //TODO: Add update logic here
            return RedirectToAction("Index");
         }catch{
            return View();
         }
      }
		
      //GET: Employee/Delete/5
      public ActionResult Delete(int id){
         return View();
      }
		
      //POST: Employee/Delete/5
      [HttpPost]
      public ActionResult Delete(int id, FormCollection collection){
         try{
            //TODO: Add delete logic here
            return RedirectToAction("Index");
         }catch{
            return View();
         }
      }
   }
}

让我们添加一个模型。

步骤11 - 在Solution Explorer中的Models文件夹上单击鼠标右键,然后选择Add→Class。

链接:https://www.learnfk.comhttps://www.learnfk.com/asp.net_mvc/asp.net-mvc-data-model.html

来源:LearnFk无涯教程网

Right-click Models Folder

您将看到"Add New Item"对话框。

Add New Item Dialog

步骤12 - 在中间窗格中选择"Class",然后在名称字段中输入Employee.cs。

步骤13 - 使用以下代码向Employee类添加一些属性。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCSimpleApp.Models {
   public class Employee{
      public int ID { get; set; }
      public string Name { get; set; }
      public DateTime JoiningDate { get; set; }
      public int Age { get; set; }
   }
}

让我们通过添加另一种方法来更新EmployeeController.cs文件,该方法将返回员工列表。

[NonAction]
public List<Employee> GetEmployeeList(){
   return new List<Employee>{
      new Employee{
         ID = 1,
         Name = "Allan",
         JoiningDate = DateTime.Parse(DateTime.Today.ToString()),
         Age = 23
      },
		
      new Employee{
         ID = 2,
         Name = "Carson",
         JoiningDate = DateTime.Parse(DateTime.Today.ToString()),
         Age = 45
      },
		
      new Employee{
         ID = 3,
         Name = "Carson",
         JoiningDate = DateTime.Parse(DateTime.Today.ToString()),
         Age = 37
      },
		
      new Employee{
         ID = 4,
         Name = "Laura",
         JoiningDate = DateTime.Parse(DateTime.Today.ToString()),
         Age = 26
      },
   };
}

步骤14 - 更新索引操作方法,如以下代码所示。

public ActionResult Index(){
   var employees = from e in GetEmployeeList()
   orderby e.ID
   select e;
   return View(employees);
}

步骤15 - 运行此应用程序,并将/employee附加到浏览器中的URL,然后按回车,您将看到以下输出。

Can't Find Index View

从上面的屏幕截图中可以看出,存在一个错误,并且该错误实际上是描述性的,告诉我们找不到索引视图。

步骤16 - 因此,要添加视图,请在"Index"操作内右键单击并选择"Add View"。

Right-click Index Action

它将显示"Add View"对话框,并将添加默认名称。

Add Default Name

步骤17 - 从"Template"下拉列表和"Enployee"下拉列表中选择列表,然后取消选中"Use a layout page"复选框,然后单击"Add"按钮。

它将在此视图中为您添加一些默认代码。

@model IEnumerable<MVCSimpleApp.Models.Employee>
@{
   Layout = null;
}

<!DOCTYPE html>
<html>
   <head>
      <meta name = "viewport" content = "width=device-width" />
      <title>Index</title>
   </head>
	
   <body>
      <p>@Html.ActionLink("Create New", "Create")</p>
         <table class = "table">
         <tr>
            <th>
               @Html.DisplayNameFor(model => model.Name)
            </th>
				
            <th>
               @Html.DisplayNameFor(model => model.JoiningDate)
            </th>
				
            <th>
               @Html.DisplayNameFor(model => model.Age)
            </th>
				
            <th></th>
         </tr>
			
         @foreach (var item in Model) {
            <tr>
               <td>
                  @Html.DisplayFor(modelItem => item.Name)
               </td>
					
               <td>
                  @Html.DisplayFor(modelItem => item.JoiningDate)
               </td>
					
               <td>
                  @Html.DisplayFor(modelItem => item.Age)
               </td>
					
               <td>
                  @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
                  @Html.ActionLink("Details", "Details", new { id = item.ID }) |
                  @Html.ActionLink("Delete", "Delete", new { id = item.ID })
               </td>
					
            </tr>
         }
			
      </table>
   </body>
</html>

步骤18 - 运行此应用程序,您将收到以下输出。

List of Employees

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Vue开发实战 -〔唐金州〕

Netty源码剖析与实战 -〔傅健〕

性能工程高手课 -〔庄振运〕

正则表达式入门课 -〔涂伟忠〕

Web安全攻防实战 -〔王昊天〕

Dubbo源码剖析与实战 -〔何辉〕

大型Android系统重构实战 -〔黄俊彬〕

Python实战 · 从0到1搭建直播视频平台 -〔Barry〕

PPT设计进阶 · 从基础操作到高级创意 -〔李金宝(Bobbie)〕

好记忆不如烂笔头。留下您的足迹吧 :)