ASP.NET MVC3手把手教你构建Web

时间:2021-05-26

开发工具:VS2010+MSSQL2005,需要使用MVC3.0

环境配置

第一步:到官方网站下载MVC3,提供了简体中文。先安装 AspNetMVC3ToolsUpdateSetup.exe,然后安装AspNetMVC3ToolsUpdateVS11Setup.exe

http://db;Persist Security Info=True;User ID=;Password=" providerName="System.Data.SqlClient" /> </connectionStrings>

第四步:创建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上右键,新建视图。勾选“创建强类型视图”,选择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"> 标题 </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>

运行后效果图如下:


到此,第一个列表页面就完成了(未涉及分页,后续会更新)。关于添加,修改,删除也就很容易了。

添加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稍微有点修改:

[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,本文也只是本人学习中的一点点积累,有很多不好的地方,希望大家多提意思。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章