关于asp 的mvcasp.net mvc 架构设计的删除文件

您所在的位置: &
ASP.NET MVC 2.0之编辑和删除新闻操作
ASP.NET MVC 2.0之编辑和删除新闻操作
本文将介绍的是ASP.NET MVC 2.0的编辑和删除新闻操作,包括创建View视图NewsEdit和NewsDelete等等。
关于ASP.NET MVC框架,51CTO-.NET频道向您推荐,本系列文章还包括
第一篇《》
第二篇《》
第三篇《》
第四篇《》
创建View视图NewsEdit和NewsDelete
创建新闻首页,用来显示新闻列表。
在Views/News目录下,单击右键,选择Add-&View,修改相关配置如下图所示
498)this.width=498;' onmousewheel = 'javascript:return big(this)' border="0" alt="修改相关配置" src="/files/uploadimg/3170.gif" />
NewsEdit View
498)this.width=498;' onmousewheel = 'javascript:return big(this)' border="0" alt="选择Add-&View" src="/files/uploadimg/3171.gif" />
NewsDelete View
在生成的HTML代码中,进行相关展示方面的修改。主要代码如下:
&ID=&Content2&&ContentPlaceHolderID=&MainContent&&runat=&server&&& &&&&&新闻编辑-&&&&&%&using&(Html.BeginForm())&{%&&&&&&&&&%:&Html.ValidationSummary(true)&%&&&&& &&&&&&&&&&&&&&&&&&&&&&Fields&&&&&&&&&&&& &&&&&&&&&&&&&&class=&editor-label&&style=&display:&&&&&&&&&&&&&&&&&&%:&Html.LabelFor(model&=&model.Id)&%&&&&&&&&&&&&&&&&&&&&&&&&&&&class=&editor-field&&style=&display:&&&&&&&&&&&&&&&&&&%:&Html.TextBoxFor(model&=&model.Id)%&&&&&&&&&&&&&&&&&%:&Html.ValidationMessageFor(model&=&model.Id)&%&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&class=&editor-label&&&&&&&&&&&&&&&&&&%:&Html.LabelFor(model&=&model.Title)&%&&&&&&&&&&&&&&&&&&&&&&&&&&&class=&editor-field&&&&&&&&&&&&&&&&&&%:&Html.TextBoxFor(model&=&model.Title)&%&&&&&&&&&&&&&&&&&%:&Html.ValidationMessageFor(model&=&model.Title)&%&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&class=&editor-label&&&&&&&&&&&&&&&&&&%:&Html.LabelFor(model&=&model.CreateTime)%&&&&&&&&&&&&&&&&&&&&&&&&&&&class=&editor-field&&&&%:&Html.TextBoxFor(model&=&model.CreateTime,new&{&@class&=&&date&&})&%&&&&&&&&&%:&Html.ValidationMessageFor(model&=&model.CreateTime)&%&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&class=&editor-label&&&&&&&&&&&&&&&&&&%:&Html.LabelFor(model&=&model.Content)&%&&&&&&&&&&&&&&&&&&&&&&&&&&&class=&editor-field&&&&&&&&&&&&&&&&&&%:&Html.EditorFor(model&=&model.Content)&%&&&&&&&&&&&&&&&&&%:&Html.ValidationMessageFor(model&=&model.Content)&%&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&type=&submit&&value=&Save&&&&&&&&&&&&&&&&&&&&&&&&&&&&&%&}&%&&&&&&&&&&&&&&%:&Html.ActionLink(&Back&to&List&,&&Index&)&%&&&&&&&
新闻编辑页面HTML代码。
隐藏style=&display:& 新闻编号Id
给日期文本框加Class=&Date&属性:new { @class = &date& },从而当用户点击日期文本框时,显示日历控件,供用户选择日期。详情,请参照文章: 《》.
删除页面NewsDelete.aspx主要代码如下:
&ID=&Content2&&ContentPlaceHolderID=&MainContent&&runat=&server&&&&&&删除新闻&&&&&确认要删除此条记录吗?&&&&&&&&&&&&&&Fields&&&&&&&& &&&&&&&&&&class=&display-label&编号:&&&&&&&&&&class=&display-field&%:&Model.Id&%&&&&&&&& &&&&&&&&&&class=&display-label&标题:&&&&&&&&&&class=&display-field&%:&Model.Title&%&&&&&&&& &&&&&&&&&&class=&display-label&创建时间:&&&class=&display-field&%:&String.Format(&{0:g}&,&Model.CreateTime)&%&& &&&&&&&&&&class=&display-label&新闻内容&&&&&&&&&&class=&display-field&%:&Model.Content&%&& &&&&&&&&&&%&using&(Html.BeginForm())&{&%&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&type=&submit&&value=&Delete&&&| &&&&&&&&&&&&&&&&&&&&&&&&%:&Html.ActionLink(&Back&to&List&,&&Index&)&%&&&&&&&&&&&&&&%&}&%&&
修改Controller文件
在Controllers/News文件下
498)this.width=498;' onmousewheel = 'javascript:return big(this)' border="0" alt="修改Controller文件" src="/files/uploadimg/3172.gif" />
修改NewsEdit.aspx页面所对应的的Action方法NewsEdit,以使NewsEdit.aspx页面初始化数据,此处未读读取数据库,而是从静态变脸集合中读取相对应的记录。
NewsEdit.asp所对应的 Action 代码如下:
&&&&&&&&&&&&&&&&&&public&ActionResult&NewsEdit(int&id) &&&&&&&&&{ &&&&&&&&&&&&&THelperMVC.Models.News.NewsModel&news&=&newsList[id]; &&&&&&&&&&&&&return&View(news); &&&&&&&&&} &[HttpPost] &&&&&&&&&&public&ActionResult&Edit(int&id,&FormCollection&collection) &&&&&&&&&{ &&&&&&&&&&&&&try&&&&&&&&&&&&&{ &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&return&RedirectToAction(&Index&); &&&&&&&&&&&&&} &&&&&&&&&&&&&catch&&&&&&&&&&&&&{ &&&&&&&&&&&&&&&&&return&View(); &&&&&&&&&&&&&} &&&&&&&&&}&
NewsDelete.Aspx所对应的Action方法,如下
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&public&ActionResult&NewsDelete(int&id) &&&&&&&&&{ &&&&&&&&&&&&&THelperMVC.Models.News.NewsModel&news&=&newsList[id]; &&&&&&&&&&&&&return&View(news); &&&&&&&&&} && &&&&&&&&&&&&&&&&&&[HttpPost] &&&&&&&&&&&&&public&ActionResult&Delete(int&id,&FormCollection&collection) &&&&&&&&&{ &&&&&&&&&&&&&try&&&&&&&&&&&&&{ &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&return&RedirectToAction(&Index&); &&&&&&&&&&&&&} &&&&&&&&&&&&&catch&&&&&&&&&&&&&{ &&&&&&&&&&&&&&&&&return&View(); &&&&&&&&&&&&&} &&&&&&&&&}&
根据URL传过来的参数(即新闻编号Id),从全局静态变量中寻找NewsModel实体,从而初始化新闻删除页面。
最后修改新闻页Index.aspx中的Edit连接,如下图所示:
此时,点击新闻页Index.aspx超链接,会寻找NewsController文件夹下的NewsEdit方法或者NewsDelete方法,从而初始化Views/News/NewsEdit.aspx页面或者Views/News/NewsDelete.aspx页面,
程序运行效果
按下Ctrl+F5运行程序,如下图所示:
498)this.width=498;' onmousewheel = 'javascript:return big(this)' border="0" alt="运行效果" src="/files/uploadimg/3173.gif" />
点击上图中的【News】超链接,跳转到新闻列表页面,如下图所示:
498)this.width=498;' onmousewheel = 'javascript:return big(this)' border="0" alt="跳转" src="/files/uploadimg/3174.gif" />
点击【Edit】超链接,会跳转到相应记录的编辑页面,如下图所示:
498)this.width=498;' onmousewheel = 'javascript:return big(this)' border="0" alt="编辑页面" src="/files/uploadimg/3175.gif" />
点击【Delete】超链接,会跳转到相应记录的删除页面,如下图所示:
498)this.width=498;' onmousewheel = 'javascript:return big(this)' border="0" alt="Delete" src="/files/uploadimg/3176.gif" />
至此,使用Asp,net MVC2.0框架完成了简单的增查改删操作。接下来,在时间允许的情况下,会对MVC2.0框架原理进行一些总结,还望园子里德朋友指点。
原文标题:Asp.net MVC2.0系列文章-编辑和删除新闻操作
链接:/ywqu/archive//1768765.html
【编辑推荐】
【责任编辑: TEL:(010)】
关于的更多文章
Angular.js 是一个MV*(Model-View-Whatever,不管是MVC或者MVVM
随着云计算、物联网、大数据、移动互联网的大发展,你应该知道这些。
又是一周匆匆而过。上周五、周六两天,2013年51CTO云
十一归来,貌似大家都没有什么劲头。本周五,为期两天
《待字闺中:编程面试题集》集合最新最全的面试经验和
SQL(结构化查询语言)是数据库系统的通用语言,利用它可以用几乎同样的语句在不同的数据库系统上执行同样的操作,在数据库系统的
51CTO旗下网站解析ASP.NET Mvc开发之删除修改数据
其实现在的Web开发通用的模式就是前端使用Js和JQuery来和后端进行数据交互。那么我们就在前端来加两个删除和更新的脚本。代码如下:
       &!--遍历 Action方法 设置给 ViewData 的集合数据,生成HTML代码--&
& & & & & & @foreach (BlogArticle a in ViewData[&DataList&] as List&BlogArticle&)
& & & & & & {
& & & & & & & & &tr&
& & & & & & & & & & &td&@a.AId&/td&
& & & & & & & & & & &td&@a.ATitle&/td&
& & & & & & & & & & &td&@a.BlogArticleCate.Name&/td&
& & & & & & & & & & &td&@a.Enumeration.e_cname&/td&
& & & & & & & & & & &td&@a.AAddtime&/td&
& & & & & & & & & & &!---------为文章列表添加删除按钮---------&&
& & & & & & & & & & &td&&a href=&javascript:del(@a.AId)&&删除&/a&&/td&
& & & & & & & & &/tr&
& & & & & & }
然后为该按钮编写Js脚本函数,代码如下:
&script type=&text/javascript&&
& & & & function del(id) {
& & & & & & if (confirm(&确定要删除么?&)) {
          &!--这里配置当用户确定删除时,js让页面跳转到的的url地址--&
& & & & & & & & window.location=&/home/del/&+
& & & & & & }
& & &/script&
上面的Js代码的意思是,当用户点击删除按钮时,url跳转的地址是&/home/del&+id
点击前的效果如下图,注意url地址是
它代表的意思是,现根据路由,请求了控制器(Controller),控制器根据路由的配置, 返回视图方法,然后试图再把Html,Js等回传给浏览器。
点击删除按钮并确定,注意url地址是
说明我们配置的路由信息是&home/del&
所以我们在控制器的HomeController中添加一个del方法,来完成删除操作,代码如下:
首先我们先来看看MVC默认的路由表配置,在App_Start文件夹下的RouteConfig.cs,代码如下:
//路由表配置
& & public class RouteConfig
& & & & public static void RegisterRoutes(RouteCollection routes)
& & & & & & routes.IgnoreRoute(&{resource}.axd/{*pathInfo}&);
& & & & & & routes.MapRoute(
& & & & & & & & name: &Default&,
& & & & & & & & url: &{controller}/{action}/{id}&,
& & & & & & & & defaults: new { controller = &Home&, action = &Index&, id = UrlParameter.Optional }
& & & & & & );
我们可以发现ur的默认配置模式是&{controller}/{action}/{id}&
默认的配置信息是defaults: new { controller = &Home&, action = &Index&, id = UrlParameter.Optional }
特别注意的是id = UrlParameter.Optional是可选的。
所以我们应该在homeController.cs中创建一个del类型的Action方法,代码如下:
&//删除文章
& & & & public ActionResult Del(int id)
& & & & & & //操作使用try...catch来捕捉异常
& & & & & & try
& & & & & & {
& & & & & & & & //创建删除对象
& & & & & & & & BlogArticle artTicle = new BlogArticle();
& & & & & & & & artTicle.AId =
& & & & & & & & //将删除对象添加到EF 对象管理容器
& & & & & & & & db.BlogArticles.Attach(artTicle);
& & & & & & & & //将对象包装类的状态标识为删除状态
& & & & & & & & db.BlogArticles.Remove(artTicle);
& & & & & & & & //更新到数据库
& & & & & & & & db.SaveChanges();
& & & & & & & & //更新成功后,页面跳转到Index页面
& & & & & & & & return RedirectToAction(&Index&, &Home&);
& & & & & & }
& & & & & & catch (Exception ex)
& & & & & & {
& & & & & & & & return RedirectToAction(&友好页面&);
& & & & & & }
& & & & & & //return View();
这样就完成了数据的删除操作。
接下来我们就来完成修改的代码,我们先展示视图(View)代码如下
&&!--生成一个表单,并且指明表单提交方法,和路由--&
& & @using (Html.BeginForm(&Modify&, &Home&, FormMethod.Post))
& & & & &table id=&tbList&&
& & & & & & &tr&
& & & & & & & & &td colspan=&2&&修改 @Html.HiddenFor(a=&a.AId) &/td&
& & & & & & &/tr&
& & & & & & &tr&
& & & & & & & & &td&标题:&/td&
& & & & & & & & @*&td&@Html.TextBox(&txtName&,(object)Model.ATitle)&/td&*@
& & & & & & & & &!--使用HtmlHelper的强类型方法 直接 从 Model 中 根据 ATitle 属性生成文本框--&
& & & & & & & & &td&@Html.TextBoxFor(a=&a.ATitle)&/td&
& & & & & & &/tr&
& & & & & & &tr&
& & & & & & & & &td&分类:&/td&
& & & & & & & & &!--使用强类型方法生成下拉框,并自动根据 model属性里的ACate值 设置 下拉框的默认选中项--&
& & & & & & & & &td&@Html.DropDownListFor(a=&a.ACate,ViewBag.CateList as IEnumerable&SelectListItem&)&/td&
& & & & & & &/tr&
& & & & & & &tr&
& & & & & & & & &td&内容:&/td&
& & & & & & & & &!--使用HtmlHelper的强类型方法 直接 从 Model 中 根据 AContent 属性生成文本域--&
& & & & & & & & &td&@Html.TextAreaFor(a =& a.AContent, 10, 60, null)&/td&
& & & & & & &/tr&
& & & & & & &tr&
& & & & & & & & &td colspan=&2&&&input type=&submit& value=&确定修改& /& @Html.ActionLink(&返回&,&Index&,&Home&)&/td&
& & & & & & &/tr&
& & & & &/table&
我们的后端代码改如何编写呢?
//因为我们这个Action是在表达以post方式提交时执行的,所以加上标识
& & & & [HttpGet]
& & & & /// &summary&
& & & & /// 加载需要修改文章
& & & & /// &/summary&
& & & & /// &param name=&id&&需要修改文章的Id&/param&
& & & & /// &returns&&/returns&
& & & & public ActionResult EditArticle(int id)
& & & & & & //获取需要编辑文章,并且返回该实体对象的第一个元素
& & & & & & BlogArticle art = (from c in db.BlogArticles where c.AId == id select c).FirstOrDefault();
& & & & & &&
& & & & & & //我们把文章的分类做成一个下拉列表,并且给DropList的&option&赋值
& & & & & & IEnumerable&SelectListItem& seleListItem = (from a in db.BlogArticleCates where a.IsDel == false select a).ToList().Select(a =& new SelectListItem { Value = a.Id.ToString(), Text = a.Name });
& & & & & & //返回List对象
& & & & & & ViewBag.CateList = seleListI
& & & & & & return View();
接下来就是执行修改的代码:
[HttpPost]
& & & & /// &summary&
& & & & /// 执行修改的代码
& & & & /// &/summary&
& & & & /// &param name=&model&&&/param&
& & & & /// &returns&&/returns&
& & & & public ActionResult Modify(BlogArticle model)
& & & & & & try
& & & & & & {
& & & & & & & & //1.将实体对象 a.加入 EF 对象容器中,并 b.获取 伪包装类对象
& & & & & & & & DbEntityEntry&BlogArticle& entry = db.Entry&BlogArticle&(model);
& & & & & & & & //2.将包装类对象的状态设置为 unchanged
& & & & & & & & entry.State = System.Data.EntityState.U
& & & & & & & & //3.设置 被改变的属性
& & & & & & & & entry.Property(a =& a.ATitle).IsModified =
& & & & & & & & entry.Property(a =& a.AContent).IsModified =
& & & & & & & & entry.Property(a =& a.ACate).IsModified =
& & & & & & & & //4.提交到数据库 完成修改
& & & & & & & & db.SaveChanges();
& & & & & & & & //5.更新成功,则命令浏览器 重定向 到 /Home/List 方法
& & & & & & & & return RedirectToAction(&Index&, &Home&);
& & & & & & }
& & & & & & catch (Exception ex)
& & & & & & {
& & & & & & & & return Content(&修改失败~~~& + ex.Message);
& & & & & & }
& & & & }&
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'}

我要回帖

更多关于 asp.net mvc网站架构 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信