在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ C#/ ASP.Net MVC數(shù)據(jù)模型
ASP.Net MVC簡介
ASP.Net MVC過濾器
ASP.Net MVC視圖
ASP.Net MVC安全
ASP.Net MVC手腳架
ASP.Net MVC控制器
ASP.Net MVC與SQL Server數(shù)據(jù)庫操作
ASP.Net MVC NuGet包管理
ASP.Net MVC入門程序
ASP.Net MVC Razor
ASP.Net MVC Bootstrap
ASP.Net MVC單元測試
ASP.Net MVC動作
ASP.Net MVC模式
ASP.Net MVC選擇器
ASP.Net MVC開發(fā)環(huán)境配置
ASP.Net MVC生命周期
ASP.Net MVC模型綁定
ASP.Net MVC自托管(本地主機部署)
ASP.Net MVC驗證
ASP.Net MVC緩存
ASP.Net MVC數(shù)據(jù)模型
ASP.Net MVC路由
ASP.Net MVC教程
ASP.Net MVC助手
ASP.Net MVC數(shù)據(jù)注解
ASP.Net MVC Web API

ASP.Net MVC數(shù)據(jù)模型

在本章中,我們將討論和學(xué)習(xí)在ASP.NET MVC Framework應(yīng)用程序中構(gòu)建模型。模型存儲數(shù)據(jù),并根據(jù)來自控制器中的命令檢索出來,最終在視圖中顯示這些數(shù)據(jù)。

Model是一個類的集合,應(yīng)用程序中可使用這些數(shù)據(jù)來編寫業(yè)務(wù)邏輯。 因此,基本上模型是業(yè)務(wù)領(lǐng)域特定的容器。它用于與數(shù)據(jù)庫進(jìn)行交互。也可以用來操縱數(shù)據(jù)來實現(xiàn)業(yè)務(wù)邏輯。

下面通過創(chuàng)建一個新的ASP.NET MVC項目,來演示如何應(yīng)用模型的簡單例子。
打開Visual Studio,然后單擊菜單:文件 -> 新建 -> 項目 選項。創(chuàng)建一個名稱為:MVCSimpleApp 的MVC項目。

詳細(xì)創(chuàng)建過程請參考:http://www.yiibai.com/asp.net_mvc/asp.net_mvc_getting_started.html

通過在解決方案資源管理器 中右鍵單擊 Controllers 文件夾來添加一個控件器:HomeController。在彈出菜單項中選擇:添加 -> 控制器 。
選擇包含讀/寫操作的MVC 5控制器 選項,這個模板將為控制器創(chuàng)建一個具有默認(rèn)操作的Index方法。這也將列出其他方法,如:Edit/Delete/Create 。

第1步 - 創(chuàng)建控制器

Controllers文件夾中看到一個新的 C# 文件 - EmployeeController.cs,在Visual Studio中打開并進(jìn)行編輯。修改更新EmployeeController.cs文件中的代碼,其中包含一些默認(rèn)的動作方法,如下面的代碼所示 -

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();
         }
      }
   }
}

第2步 - 添加模型

右鍵單擊解決方案資源管理器 中的Models 文件夾,然后在彈出菜單項中選擇:添加 -> , 將看到添加新項目對話框,填寫模型名稱為:Employee.cs ,如下圖所示 -

第3步 - 使用下面的代碼將一些屬性添加到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
      },
   };
}

第4步 - 更新索引操作方法,如下面的代碼所示。

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

第5步 - 運行這個應(yīng)用程序,打開瀏覽器訪問URL:http://localhost:64466/employee,將看到以下輸出。

正如在上面的截圖所看到的,有一個錯誤,這個錯誤實際上是相當(dāng)具有描述性的,告訴我們它找不到索引視圖。

第6步 - 因此,要添加一個視圖,右鍵單擊Index動作方法,并選擇添加視圖。它將顯示“添加視圖”對話框,并將添加默認(rèn)名稱。參考下圖 -

第7步 - 從模板下拉列表中選擇列表,在模型類下拉列表中選擇Employee,并取消選中“使用布局頁面”復(fù)選框,然后單擊“添加” 按鈕。

它會在這個視圖中自動添加一些默認(rèn)的代碼。如下所示 -

@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>

第8步 - 運行這個應(yīng)用程序,將看到以下輸出。