ASP.NET MVC 4.0 学习2-留言板實現
新增專案實現留言板功能,瞭解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實現留言板的添加留言查看留言的功能:
- Index:調用Service中的GetData方法,返回Article列表
- Create:顯示頁面
- 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-留言板實現的更多相关文章
- ASP.NET MVC 4.0 学习5-ActionResult
一,Controller簡介 Controller擔任了資料傳遞的角色,負責流程控制,決定存取哪個Model以及決定顯示哪個View頁面,即ASP.NET MVC中有關於『傳遞』的任務皆由Contro ...
- ASP.NET MVC 4.0 学习6-Model Binding
一,ViewData,ViewBag與TempData ASP.NET MVC架構中,通過繼承在Controller中的ViewData,ViewBag和TempData和View頁面進行資料的存取, ...
- ASP.NET MVC 4.0 学习1-C#基础语法
1,方法多載,相同的方法名稱,不同的參數類型.數量 class Program { static void Main(string[] args) { Program newObject = new ...
- ASP.NET MVC 4.0 学习4-Code First
之前我們需要用到的數據,通過添加Entity實體數據模型把數據庫中需要的Database拉到項目中如下圖, 而就是Code First就是相對於這種處理數據的方法而言的 Code First更加準確的 ...
- ASP.NET MVC 4.0 学习3-Model
Model負責獲取數據庫中的資料,並對數據庫中的數據進行處理. MVC中有關 數據庫 的任務都由Model來完成,Model中對數據資料進行定義,Controller和View中都會參考到Model, ...
- 从零开始学习ASP.NET MVC 1.0
转自:http://www.cnblogs.com/zhangziqiu/archive/2009/02/27/ASPNET-MVC-1.html <从零开始学习ASP.NET MVC 1.0& ...
- 系列文章--从零开始学习ASP.NET MVC 1.0
从零开始学习ASP.NET MVC 1.0 (一) 开天辟地入门篇 从零开始学习 ASP.NET MVC 1.0 (二) 识别URL的Routing组件 从零开始学习 ASP.NET MVC 1.0 ...
- 2.第一个ASP.NET MVC 5.0应用程序
大家好,上一篇对ASP.NET MVC 有了一个基本的认识之后,这一篇,我们来看下怎么从头到尾创建一个ASP.NET MVC 应用程序吧.[PS:返回上一篇文章:1.开始学习ASP.NET MVC] ...
- Profession ASP.NET MVC 2.0 NerdDinner示例可运行源码
最近一段时间在看JonGalloway等著作的<Profession ASP.NET MVC 2.0>.本书并没有按照常规的大部头书籍那样,按部就班的介绍MVC的概念等,而是在第一章直接引 ...
随机推荐
- Linux下使用VirtualBox安装Windows系统
(文档比较长,只是写的详细,实际操作起来相对简单.) 由于一些特殊原因,我们并不能完全抛下Windows而使用Linux.VirtualBox 是一款虚拟机软件,支持多系统.在Linux下安装 Vir ...
- content的定义
http://www.myexception.cn/HTML-CSS/1472528.html http://stackoverflow.com/questions/2770681/css-conte ...
- HDU 4035 Maze(树形概率DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4035 题意:一棵树,从结点1出发,在每个结点 i 都有3种可能:(1)回到结点1 , 概率 Ki:(2 ...
- Linux下Bash运行脚本
命令行应该这样写: sh -c "脚本字符串" example: sh -c "if ! type dpkg > /dev/null ; then echo 'so ...
- MySQL强制性操作
1.强制索引FORCE INDEX SELECT * FROM TABLE1 FORCE INDEX (FIELD1) … 只使用建立在FIELD1上的索引,而不使用其它字段上的索引. 2.忽略索引 ...
- 字符串转换为float<2>
Configuration OK zjtest7-frontend:/usr/local/logstash-2.3.4/config# ../bin/logstash -f g01.conf Sett ...
- SuperSpider——打造功能强大的爬虫利器
SuperSpider——打造功能强大的爬虫利器 1.爬虫的介绍 图1-1 爬虫(spider) 网络爬虫(web spider)是一个自动的通过网络抓取互联网 上的网页的程序,在当今互联网 中 ...
- sql server 2008 在与 SQL Server 提示建立连接时出现与网络相关的或特定于实例的错误
原文地址:http://zhidao.baidu.com/link?url=Ndav32DO9zL5XnltqoqlhvKHbJv_n3Zwihhw4cwF9ffNq8hb8z7h7n3vJVfoeW ...
- Ubuntu Eclipse的Tomcat小问题:不能输入server name,不能启动tomcat
Ubuntu的Eclipse上安装Tomcat环境,这是让人烦啊,万幸还是终于解决了. Eclipse上Tomcat的搭建: 1.点击Eclipse上的菜单:Windows - Preference, ...
- 《Java程序员面试笔试宝典》之switch使用时有哪些注意事项
switch语句用于多分支选择,在使用switch(expr)的时候,expr只能是一个枚举常量(内部也是由整型或字符类型实现)或一个整数表达式,其中整数表达式可以是基本类型int或其对应的包装类In ...