Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to edit and delete news operations in ASP.NET MVC 2.0

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how to edit and delete news operations in ASP.NET MVC 2.0. the editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

Create View views NewsEdit and NewsDelete

Create a news home page to display a list of news.

Under the Views/News directory, right-click, select Add- > View, and modify the configuration as shown in the following figure

NewsEdit View

NewsDelete View

Make changes to the presentation in the generated HTML code. The main code is as follows:

News Editor-Fields model.Id)% > model.Id)% > model.Id)% > Model.Title)% > model.CreateTime)% > model.CreateTime New {@ class = "date"})% > model.CreateTime) > model.Content)% > model.Content)% > model.Content)% >

News editor page HTML code.

Hide style= "display:none;" news number Id

Add the Class= "Date" attribute to the date text box: new {@ class = "date"}, so that when the user clicks on the date text box, the calendar control is displayed for the user to select the date.

The main code to delete the page NewsDelete.aspx is as follows:

Delete news are you sure you want to delete this record? Fields number: title: creation time: news content

| |

Modify the Controller file

Under the Controllers/News file

Modify the Action method NewsEdit corresponding to the NewsEdit.aspx page to make the NewsEdit.aspx page initialize the data, where the database is not read, but the corresponding records are read from the static face-changing collection.

The Action code for NewsEdit.asp is as follows:

/ / Edit page initialization method / / GET: / News/Edit/5 public ActionResult NewsEdit (int id) {THelperMVC.Models.News.NewsModel news = newsList [id]; return View (news) } [HttpPost] / / when the edit button is clicked, the triggered method public ActionResult Edit (int id, FormCollection collection) {try {/ / TODO: add update business logic return RedirectToAction ("Index") } catch {return View ();}}

The Action method corresponding to NewsDelete.Aspx is as follows

/ / GET: / News/Delete/5 / when the page is initialized, the parameter ID value in the triggered method / URL / News entity object public ActionResult NewsDelete (int id) {THelperMVC.Models.News.NewsModel news = newsList [id]; return View (news) } / / POST: / News/Delete/5 [HttpPost] / / the method public ActionResult Delete (int id, FormCollection collection) {try {/ / TODO: add and delete business logic return RedirectToAction ("delete") when you click the [delete] button } catch {return View ();}}

According to the parameters passed by URL (that is, the news number Id), look for the NewsModel entity from the global static variable to initialize the news deletion page.

* modify the Edit connection in the news page Index.aspx, as shown in the following figure:

At this point, click the Index.aspx hyperlink of the news page, and you will find the NewsEdit method or NewsDelete method under the NewsController folder to initialize the Views/News/NewsEdit.aspx page or Views/News/NewsDelete.aspx page.

Program running effect

Press Ctrl+F5 to run the program, as shown in the following figure:

Click the [News] hyperlink in the above figure to jump to the news list page, as shown in the following figure:

Click the [Edit] hyperlink to jump to the editing page of the corresponding record, as shown in the following figure:

Click the [Delete] hyperlink to jump to the deletion page of the corresponding record, as shown in the following figure:

This is how to edit and delete news operations in ASP.NET MVC 2.0. the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report