Asp.net MVC2.0系列文章-编辑和删除新闻操作
上一篇文章,我們簡(jiǎn)單地完成了新聞內(nèi)容的展示功能(Asp.net MVC2.0系列文章-顯示列表和詳細(xì)頁(yè)面操作),此篇文章,我們使用Asp.net MVC2.0實(shí)現(xiàn)新聞?dòng)涗浀木庉嫼蛣h除功能。
創(chuàng)建View視圖NewsEdit和NewsDelete
創(chuàng)建新聞首頁(yè),用來顯示新聞列表。
在Views/News目錄下,單擊右鍵,選擇Add->View,修改相關(guān)配置如下圖所示
NewsEdit View
NewsDelete View
在生成的HTML代碼中,進(jìn)行相關(guān)展示方面的修改。主要代碼如下:
?
| <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> ? ??? <h2>新聞編輯-</h2> ??? <% using (Html.BeginForm()) {%> ??????? <%: Html.ValidationSummary(true) %>????? ??????? <fieldset> ??????????? <legend>Fields</legend> ??????????? ??????????? <div class="editor-label" style="display:none;"> ??????????????? <%: Html.LabelFor(model => model.Id) %> ??????????? </div> ??????????? <div class="editor-field" style="display:none;"> ??????????????? <%: Html.TextBoxFor(model => model.Id)%> ??????????????? <%: Html.ValidationMessageFor(model => model.Id) %> ??????????? </div> ??????????? ??????????? <div class="editor-label"> ??????????????? <%: Html.LabelFor(model => model.Title) %> ??????????? </div> ??????????? <div class="editor-field"> ??????????????? <%: Html.TextBoxFor(model => model.Title) %> ??????????????? <%: Html.ValidationMessageFor(model => model.Title) %> ??????????? </div> ??????????? ??????????? <div class="editor-label"> ??????????????? <%: Html.LabelFor(model => model.CreateTime)%> ??????????? </div> ??????????? <div class="editor-field"> ??????????????? <%: Html.TextBoxFor(model => model.CreateTime,new { @class = "date" }) %> ??????????????? <%: Html.ValidationMessageFor(model => model.CreateTime) %> ??????????? </div> ??????????? ??????????? <div class="editor-label"> ??????????????? <%: Html.LabelFor(model => model.Content) %> ??????????? </div> ??????????? <div class="editor-field"> ??????????????? <%: Html.EditorFor(model => model.Content) %> ??????????????? <%: Html.ValidationMessageFor(model => model.Content) %> ??????????? </div> ??????????? ??????????? <p> ??????????????? <input type="submit" value="Save" /> ??????????? </p> ??????? </fieldset> ??? <% } %> ??? <div> ??????? <%: Html.ActionLink("Back to List", "Index") %> ??? </div> </asp:Content> |
?
新聞編輯頁(yè)面HTML代碼。
隱藏style="display:none;" 新聞編號(hào)Id
給日期文本框加Class=”Date”屬性:new { @class = "date" },從而當(dāng)用戶點(diǎn)擊日期文本框時(shí),顯示日歷控件,供用戶選擇日期。詳情,請(qǐng)參照文章: Asp.net MVC2.0系列文章-添加操作.
?
刪除頁(yè)面NewsDelete.aspx主要代碼如下:
?
| <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> ??? <h2>刪除新聞</h2> ??? <h3>確認(rèn)要?jiǎng)h除此條記錄嗎?</h3> ??? <fieldset> ??????? <legend>Fields</legend> ??????? ??????? <div class="display-label">編號(hào):</div> ??????? <div class="display-field"><%: Model.Id %></div> ??????? ??????? <div class="display-label">標(biāo)題:</div> ??????? <div class="display-field"><%: Model.Title %></div> ??????? ??????? <div class="display-label">創(chuàng)建時(shí)間:</div> ??????? <div class="display-field"><%: String.Format("{0:g}", Model.CreateTime) %></div> ??????? ??????? <div class="display-label">新聞內(nèi)容</div> ??????? <div class="display-field"><%: Model.Content %></div> ??????? ??? </fieldset> ??? <% using (Html.BeginForm()) { %> ??????? <p> ?????????????????? ??? <input type="submit" value="Delete" /> | ?????????????????? ??? <%: Html.ActionLink("Back to List", "Index") %> ??????? </p> ??? <% } %> </asp:Content> |
修改Controller文件
在Controllers/News文件下
修改NewsEdit.aspx頁(yè)面所對(duì)應(yīng)的的Action方法NewsEdit,以使NewsEdit.aspx頁(yè)面初始化數(shù)據(jù),此處未讀讀取數(shù)據(jù)庫(kù),而是從靜態(tài)變臉集合中讀取相對(duì)應(yīng)的記錄。
NewsEdit.asp所對(duì)應(yīng)的 Action 代碼如下:
?
| //編輯頁(yè)面初始化方法 ??????? // GET: /News/Edit/5 ??????? public ActionResult NewsEdit(int id) ??????? { ??????????? THelperMVC.Models.News.NewsModel news = newsList[id]; ??????????? return View(news); ??????? } [HttpPost] //點(diǎn)擊編輯按鈕時(shí),觸發(fā)的方法 ??????? public ActionResult Edit(int id, FormCollection collection) ??????? { ??????????? try ??????????? { ??????????????? // TODO: 添加更新業(yè)務(wù)邏輯 ??????????????? return RedirectToAction("Index"); ??????????? } ??????????? catch ??????????? { ??????????????? return View(); ??????????? } ??????? } |
?
NewsDelete.Aspx所對(duì)應(yīng)的Action方法,如下
?
| // GET: /News/Delete/5 ??????? /// <summary> ??????? /// 頁(yè)面初始化時(shí),觸發(fā)的方法 ??????? /// </summary> ??????? /// <param name="id">URL中的參數(shù)Id值</param> ??????? /// <returns>新聞實(shí)體對(duì)象</returns> ??????? public ActionResult NewsDelete(int id) ??????? { ??????????? THelperMVC.Models.News.NewsModel news = newsList[id]; ??????????? return View(news); ??????? } ? ??????? // POST: /News/Delete/5 ??????? [HttpPost] ??????? //點(diǎn)擊【刪除】按鈕時(shí)觸發(fā)的方法 ??????? public ActionResult Delete(int id, FormCollection collection) ??????? { ??????????? try ??????????? { ??????????????? // TODO: 添加刪除業(yè)務(wù)邏輯 ??????????????? return RedirectToAction("Index"); ??????????? } ??????????? catch ??????????? { ??????????????? return View(); ??????????? } ??????? } |
?
根據(jù)URL傳過來的參數(shù)(即新聞編號(hào)Id),從全局靜態(tài)變量中尋找NewsModel實(shí)體,從而初始化新聞刪除頁(yè)面。
最后修改新聞頁(yè)Index.aspx中的Edit連接,如下圖所示:
此時(shí),點(diǎn)擊新聞頁(yè)Index.aspx超鏈接,會(huì)尋找NewsController文件夾下的NewsEdit方法或者NewsDelete方法,從而初始化Views/News/NewsEdit.aspx頁(yè)面或者Views/News/NewsDelete.aspx頁(yè)面,
程序運(yùn)行效果
按下Ctrl+F5運(yùn)行程序,如下圖所示:
點(diǎn)擊上圖中的【News】超鏈接,跳轉(zhuǎn)到新聞列表頁(yè)面,如下圖所示:
點(diǎn)擊【Edit】超鏈接,會(huì)跳轉(zhuǎn)到相應(yīng)記錄的編輯頁(yè)面,如下圖所示:
點(diǎn)擊【Delete】超鏈接,會(huì)跳轉(zhuǎn)到相應(yīng)記錄的刪除頁(yè)面,如下圖所示:
總結(jié)
至此,使用Asp,net MVC2.0框架完成了簡(jiǎn)單的增查改刪操作。接下來,在時(shí)間允許的情況下,會(huì)對(duì)MVC2.0框架原理進(jìn)行一些總結(jié),還望園子里德朋友指點(diǎn)。
?
總結(jié)
以上是生活随笔為你收集整理的Asp.net MVC2.0系列文章-编辑和删除新闻操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在MFC框架下使用osg报内存泄露的解决
- 下一篇: Windows7 IIS7下以FastC