新增專案實現留言板功能,瞭解MVC的運行機制

1,新增專案

 

2,添加數據庫文件message.mdf

 

Ctrl+W,L 打開資料庫連接,添加存放留言的Atricle表

添加字段,後點擊"更新"後看到新增的Atricle表(Content 應該設置為text)

3,添加ADO.NET實體數據模型 (MVC通過實體數據模型對數據庫中的數據進行增删改查)

    

     

  ADO.NET實體數據模型添加完成。

4,建立Service

我們把對Model中message.mdf數據處理的類單獨放在Service文件夾中,這樣更加方便之後的維護同樣也符合MVC耦合度低的特點,這一步是為Controller中的Action方法做準備。新建Service文件夾,添加messageDBService.cs類(Entity實體和Controller的橋樑):

  

Service類中添加兩個方法,分別實現對數據的讀和寫

  • GetData():讀取並返回數據庫中Article中的數據
  • DBCreate():把接收的數據存放到Article表中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcApplication1.Models; //引用Model命名空間 namespace MvcApplication1.Service
{
public class messageDBService
{
//實例化實體數據
public Models.messageEntities db=new Models.messageEntities(); //讀取並返回messageEntity中的數據
public List<Article> GetData()
{
return (db.Article.ToList());
}
//把從User接受的數據寫入messageEnitity
public void DBCreate(string strTitle,string strContent)
{
//實例化Artile對象
Article newData=new Article(); //給Artile對象的屬性賦值
newData.Title=strTitle;
newData.Content=strContent;
newData.time=DateTime.Now; //實體添加到Entity中
db.Article.Add(newData);
//保存到數據庫
db.SaveChanges(); }
}
}

4,添加控制器Controller
 

控制器中的Action實現留言板的添加留言查看留言的功能:

  1. Index:調用Service中的GetData方法,返回Article列表
  2. Create:顯示頁面
  3. Create:方法前有[HttpPost]屬性,只有瀏覽器發送POST請求的時候才會執行此方法,調用Service中的CreateDB方法把數據寫到Article表中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
using MvcApplication1.Service; namespace MvcApplication1.Controllers
{
public class messageController : Controller
{
//實例化Service
messageDBService data = new messageDBService(); // GET: /message/
public ActionResult Index()
{
//Article列表
return View(data.GetData());
} public ActionResult Create()
{
return View();
} [HttpPost]
public ActionResult Create(string strAtricle, string strContent)
{
//調用Service中的Create方法,把數據寫到數據庫中
data.DBCreate(strAtricle, strContent);
//重導向到 Action
return RedirectToAction("Index");
}
}
}

5,添加Action對應的View頁面:

View頁面接收Controller傳遞過來的資料,在User提交按鈕的時候把數據傳給Controller

View Index顯示Article內容,更新View如下:

@model IEnumerable<MvcApplication1.Models.Article>

@{
ViewBag.Title = "留言板";
}
<div> <h2>首頁-留言列表</h2> @if (Model != null)
{
<table>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>標題:</td>
<td>@item.Title</td>
</tr>
<tr>
<td>留言內容:</td>
<td>@item.Content</td>
</tr>
<tr>
<td>時間:</td>
<td>@item.time</td>
</tr>
}
</tbody>
</table>
} </div>
<br />
<div>
@Html.ActionLink("點擊新增留言", "Create");
</div>

同樣為 Controller中的 Create Action 添加對應的View:

@model IEnumerable<MvcApplication1.Models.Article>
@{
ViewBag.Title = "Create";
} <h2>新增留言</h2>
@using (Html.BeginForm("Create", "Message"))
{
<div>
@Html.Label("標題")
@Html.TextBox("strAtricle")
<br /> @Html.Label("內容")
@Html.TextBox("strContent")
<br />
<input type="submit" value="送出留言" />
</div> }

在~/Views/Shared/_Layout.cshtml(相當於asp.net中的母版頁Master),中添加留言首頁的連接:

  <ul id="menu">
<li>@Html.ActionLink("首頁", "Index", "Home")</li>
<li>@Html.ActionLink("關於", "About", "Home")</li>
<li>@Html.ActionLink("連絡", "Contact", "Home")</li>
<li>@Html.ActionLink("留言", "index", "Message")</li>
</ul>

以上完成了一個簡單的留言板。

首頁:

點擊"新增留言", @Html.ActionLink("點擊新增留言", "Create"); 程式根據Routing規則找到對應Controller中的Action

  → →保存到DB


通過留言板我們看到了MVC項目的運行流程如下:

補充:

按照上面的步驟運行,在新增第二條留言的時候會提示主鍵錯誤

原因:Atricle的ID主鍵沒有設置為 自增長

修正步驟:

1,把ID主鍵設置為自增長後點擊"更新"

2,從資料庫更新Model1.edmx文件

2,

3,運行:

ASP.NET MVC 4.0 学习2-留言板實現的更多相关文章

  1. ASP.NET MVC 4.0 学习5-ActionResult

    一,Controller簡介 Controller擔任了資料傳遞的角色,負責流程控制,決定存取哪個Model以及決定顯示哪個View頁面,即ASP.NET MVC中有關於『傳遞』的任務皆由Contro ...

  2. ASP.NET MVC 4.0 学习6-Model Binding

    一,ViewData,ViewBag與TempData ASP.NET MVC架構中,通過繼承在Controller中的ViewData,ViewBag和TempData和View頁面進行資料的存取, ...

  3. ASP.NET MVC 4.0 学习1-C#基础语法

    1,方法多載,相同的方法名稱,不同的參數類型.數量 class Program { static void Main(string[] args) { Program newObject = new ...

  4. ASP.NET MVC 4.0 学习4-Code First

    之前我們需要用到的數據,通過添加Entity實體數據模型把數據庫中需要的Database拉到項目中如下圖, 而就是Code First就是相對於這種處理數據的方法而言的 Code First更加準確的 ...

  5. ASP.NET MVC 4.0 学习3-Model

    Model負責獲取數據庫中的資料,並對數據庫中的數據進行處理. MVC中有關 數據庫 的任務都由Model來完成,Model中對數據資料進行定義,Controller和View中都會參考到Model, ...

  6. 从零开始学习ASP.NET MVC 1.0

    转自:http://www.cnblogs.com/zhangziqiu/archive/2009/02/27/ASPNET-MVC-1.html <从零开始学习ASP.NET MVC 1.0& ...

  7. 系列文章--从零开始学习ASP.NET MVC 1.0

    从零开始学习ASP.NET MVC 1.0 (一) 开天辟地入门篇 从零开始学习 ASP.NET MVC 1.0 (二) 识别URL的Routing组件 从零开始学习 ASP.NET MVC 1.0 ...

  8. 2.第一个ASP.NET MVC 5.0应用程序

    大家好,上一篇对ASP.NET MVC 有了一个基本的认识之后,这一篇,我们来看下怎么从头到尾创建一个ASP.NET MVC 应用程序吧.[PS:返回上一篇文章:1.开始学习ASP.NET MVC] ...

  9. Profession ASP.NET MVC 2.0 NerdDinner示例可运行源码

    最近一段时间在看JonGalloway等著作的<Profession ASP.NET MVC 2.0>.本书并没有按照常规的大部头书籍那样,按部就班的介绍MVC的概念等,而是在第一章直接引 ...

随机推荐

  1. Excel——使用VLOOKUP函数关联跨工作薄数据

    实验环境 有两个工作簿,一个是<花名册>,另一个是<入离职表>,<花名册>上有所有员工的详细信息,包括员工的姓名.部门.出生日期等,<入离职表>上有离职 ...

  2. c#添加事物(全部执行和带保存点的执行)

    全部执行 protected void Button2_Click(object sender, EventArgs e) { // 执行事务 SqlConnection con = new SqlC ...

  3. NSScanner-备

    注意:在扫描的时候,如果 空格是不需要扫描的,那么将会忽略空格. 如下代码:  1  NSString *string = @"my age is d 23    34.0";   ...

  4. LODS LODSB LODSW LODSD 例子【载入串指令】

    http://qwop.iteye.com/blog/1958761 // lodsb.cpp : Defines the entry point for the console applicatio ...

  5. opencart修改后台文件夹名

    在使用opencart进行二次开发时,若需要修改后台目录的文件夹名是可以操作的.具体步骤如下: 1.将网站后台文件夹名字改成opencartadmin 2.在该文件夹下找到config.php文件如图 ...

  6. 【HDU1232】畅通工程(并查集基础题)

    裸敲并查集,很水一次AC #include <iostream> #include <cstring> #include <cstdlib> #include &l ...

  7. flex渐变色制作圆角橙色按钮

    <?xml version="1.0" encoding="utf-8"?> <s:SparkButtonSkin xmlns:fx=&quo ...

  8. [置顶] Objective-C ,ios,iphone开发基础:自定义控件:Eg: UIButton

    第一步:新建一个工程,在 .h文件中坐如下声明: #import <UIKit/UIKit.h> @interface MyButtonViewController : UIViewCon ...

  9. pyqt labe界面超级链接例子学习

    def bz(self): self.lable1=QtGui.QLabel(u'<br><a href=http://windows.microsoft.com/zh-cn/win ...

  10. Top 15 Tools To Make Animated GIFs From Images & Video

    Creating an animated GIF picture from photos or video with Adobe Photoshop is easy, but not everyone ...