ASP.NET MVC3手把手教你構(gòu)建Web
開發(fā)工具:VS2010+MSSQL2005,需要使用MVC3.0
環(huán)境配置
第一步:到官方網(wǎng)站下載MVC3,提供了簡體中文。先安裝 AspNetMVC3ToolsUpdateSetup.exe,然后安裝AspNetMVC3ToolsUpdateVS11Setup.exe
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=1491

第二步:新建數(shù)據(jù)庫,創(chuàng)建測試表。然后往表里insert些測試數(shù)據(jù)
USE [yanComdb] GO /****** 對象: Table [dbo].[NewsEntity] 腳本日期: 03/12/2012 22:03:59 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[NewsEntity]( [NId] [int] IDENTITY(1,1) NOT NULL, [Title] [nvarchar](100) COLLATE Chinese_PRC_CI_AS NOT NULL, [Information] [text] COLLATE Chinese_PRC_CI_AS NULL, [Time] [datetime] NOT NULL CONSTRAINT [DF_NewsEntity_Time] DEFAULT (getdate()), CONSTRAINT [PK_NewsEntity] PRIMARY KEY CLUSTERED ( [NId] ASC )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
構(gòu)建列表頁面
第一步:打開VS,新建選擇MVC3 web應(yīng)用程序,輸入項(xiàng)目名稱以及目錄


第二步:創(chuàng)建NewsEntity類,本文使用自己手寫實(shí)體類(沒有使用LinqtoSql,EF等orm)
[TableAttribute("NewsEntity")]//這行很重要,因?yàn)閙vc框架默認(rèn)去db中找類名復(fù)數(shù)的表名
public class NewsEntity
{
[Key]//設(shè)置主鍵
public int NId { get; set; }
[StringLength(100)]//設(shè)置驗(yàn)證信息
[Required(ErrorMessage="標(biāo)題不能為空")]
[DisplayName("標(biāo)題")]
public string Title { get; set; }
[Required(ErrorMessage = "正文必須填寫")]
[DisplayName("正文")]
public string Information { get; set; }
public DateTime Time { get; set; }
} 第三步:配置數(shù)據(jù)庫連接字符,注意此處的name對應(yīng)下一步中創(chuàng)建的類名。
<connectionStrings> <add name="ProjectEntity" connectionString="Data Source=ip;Initial Catalog=yanComdb;Persist Security Info=True;User ID=;Password=" providerName="System.Data.SqlClient" /> </connectionStrings>
第四步:創(chuàng)建ProjectEntity類,需要繼承DbContext
public class ProjectEntity : DbContext
{
public DbSet<NewsEntity> NewsEntity { get; set; }
}
第五步:新建Controller,
ProjectEntity PE = new ProjectEntity();
public ActionResult News()
{
try
{
var list = PE.NewsEntity.ToList();
return View(list);
}
catch (Exception e)
{
throw e;
}
}
第六步:在News上右鍵,新建視圖。勾選“創(chuàng)建強(qiáng)類型視圖”,選擇NewsEntity,支架模塊選擇List

添加后,cshtml代碼如下:
@model IEnumerable<TaiQiu.Models.NewsEntity>
@{
ViewBag.Title = "后臺新聞管理列表";
Layout = "~/Views/Shared/_MLayout.cshtml";
}
<h2>
新聞列表</h2>
<p>
@Html.ActionLink("添加", "Create")
</p>
<table>
<tr>
<th width="50px">
ID
</th>
<th width="300px">
標(biāo)題
</th>
<th width="150px">
時間
</th>
<th>
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.NId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Time)
</td>
<td>
@Html.ActionLink("編輯", "EditNews", new { id = item.NId }) |
@Html.ActionLink("刪除", "DeleteNews", new { id=item.NId })
</td>
</tr>
}
</table>
運(yùn)行后效果圖如下:

到此,第一個列表頁面就完成了(未涉及分頁,后續(xù)會更新)。關(guān)于添加,修改,刪除也就很容易了。
添加Controller代碼:
[HttpPost]
[ValidateInput(false)]
public ActionResult Create(NewsEntity news)
{
if (ModelState.IsValid)
{
news.Time = DateTime.Now;
PE.NewsEntity.Add(news);
try
{
PE.SaveChanges();
return RedirectToAction("News");
}
catch (Exception e)
{
throw e;
}
}
return View();
}
添加頁面:
@model TaiQiu.Models.NewsEntity
@{
ViewBag.Title = "添加新聞";
Layout = "~/Views/Shared/_MLayout.cshtml";
}
<h2>
添加新聞</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/kindeditor/kindeditor.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/kindeditor/lang/zh_CN.js")" type="text/javascript"></script>
<script type="text/javascript">
var editor;
KindEditor.ready(function (K) {
editor = K.create('textarea[name="Information"]', {
allowFileManager: true
});
});
</script>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>News</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Title, new { style = "width:500px" })
@Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Information)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.Information, new { style="width:800px;height:400px"})
@Html.ValidationMessageFor(model => model.Information)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("返回列表", "Index")
</div> 修改頁面一樣,Controller稍微有點(diǎn)修改:
[HttpPost]
[ValidateInput(false)]
public ActionResult EditNews(NewsEntity news)
{
if (ModelState.IsValid)
{
news.Time = DateTime.Now;
PE.Entry(news).State = EntityState.Modified;//修改
PE.SaveChanges();
return RedirectToAction("News");
}
return View(news);
}
刪除Controller代碼:
public ActionResult DeleteNews(int id)
{
var model = PE.NewsEntity.Find(id);
PE.NewsEntity.Remove(model);
PE.SaveChanges();
return RedirectToAction("News");
}
小編剛接觸MVC3,本文也只是本人學(xué)習(xí)中的一點(diǎn)點(diǎn)積累,有很多不好的地方,希望大家多提意思。
- 使用基于Node.js的構(gòu)建工具Grunt來發(fā)布ASP.NET MVC項(xiàng)目
- ASP.NET性能優(yōu)化之構(gòu)建自定義文件緩存
- Asp.net TreeView來構(gòu)建用戶選擇輸入的方法 推薦
- ASP.NET2.0+SQL Server2005構(gòu)建多層應(yīng)用
- ASP.NET MVC+EF框架+EasyUI實(shí)現(xiàn)權(quán)限管系列
- ASP.NET中的Inherits、CodeFile、CodeBehind的區(qū)別詳解
- asp.net(c#)ref,out ,params的區(qū)別
- asp.net TemplateField模板中的Bind方法和Eval方法
- ASP.NET Ref和Out關(guān)鍵字區(qū)別分析
- ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后臺管理系統(tǒng)之前端頁面框架構(gòu)建源碼分享
相關(guān)文章
.NET 下運(yùn)用策略模式(組合行為和實(shí)體的一種模式)
我簡單的理解策略模式就是把行為(方法)單獨(dú)的抽象出來,并采用組合(Has-a)的方式,來組合行為和實(shí)體的一種模式比如,.NET中對數(shù)組排序的Sort的方法就是一個策略模式的實(shí)現(xiàn)模板2012-12-12
.NET必知的EventCounters性能指標(biāo)監(jiān)視器詳解
這篇文章主要介紹了.NET必知的EventCounters性能指標(biāo)監(jiān)視器,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
.NET 6開發(fā)TodoList應(yīng)用之實(shí)現(xiàn)ActionFilter
Filter在.NET Web API項(xiàng)目開發(fā)中也是很重要的一個概念,它運(yùn)行在執(zhí)行MVC響應(yīng)的Pipeline中執(zhí)行,允許我們將一些可以在多個Action之間重用的邏輯抽取出來集中管理。本文將詳細(xì)介紹一下.NET 6如何實(shí)現(xiàn)ActionFilter,感興趣的可以學(xué)習(xí)一下2021-12-12
一步步打造簡單的MVC電商網(wǎng)站BooksStore(3)
這篇文章主要和大家一起一步步打造一個簡單的MVC電商網(wǎng)站,MVC電商網(wǎng)站BooksStore第三篇,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
HttpWebRequest的常見錯誤使用TcpClient可避免
有時使用HttpWebRequest對象會出現(xiàn)錯誤有三種服務(wù)器提交了協(xié)議沖突/基礎(chǔ)連接已經(jīng)關(guān)閉:連接被意外關(guān)閉/無法發(fā)送具有此謂詞類型的內(nèi)容正文,感興趣的朋友可以參考下本文2013-02-02
visual studio 2012安裝配置方法圖文教程 附opencv配置教程
這篇文章主要為大家分享了visual studio 2012安裝配置方法圖文教程,文中附opencv配置教程,文中安裝步驟介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05
asp.net(c#)ref,out ,params的區(qū)別
C#中有三個關(guān)鍵字-ref,out ,params,雖然本人不喜歡這三個關(guān)鍵字,因?yàn)樗鼈円伤破茐拿嫦驅(qū)ο筇匦浴5羌热籱$把融入在c#體系中,那么我們就來認(rèn)識一下參數(shù)修飾符ref,out ,params吧,還有它們的區(qū)別。2009-12-12

