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 delete ASP.NET MVC jQuery

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this issue, the editor will bring you about how to delete ASP.NET MVC jQuery. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

In a recent post, Stephen Walther pointed out the dangers of using a hyperlink to delete data. This article makes a good exposition on this issue. However, such problems are not limited to delete operations. Any time you allow a GET request to modify the data, you may be asking for trouble. Here is a story that happened to BackPack a long time ago. You can read it and see what I mean.

The reason why jQuery deletions deserve our special attention is that this is one of the most common situations where hyperlinks are used to modify information. If you want to modify a product record, you may use a table (form). However, deletion usually requires only one parameter (that is, the id of the record to be deleted), and it is easy to include this parameter in the URL of the GET request.

If you are using jQuery, an easy way to convert any hyperlink into a POST connection is by assigning the following code to the value of the onclick property:

$.post (this.href); return false

such as

< a href="/go/delete/1" onclick=$.post(this.href); return false;">

Delete

< /a>

This code sends a POST request to / go/delete/1 instead of a GET request. Of course, you also need to make the necessary rules on the server side, which is simple in asp.net mvc:

[AcceptVerbs (HttpVerbs.Post)] public ActionResult Delete (int id) {/ / Delete that stuff!}

The AcceptVerbs attribute specifies that the action method only responds to the POST request, while the GET request is ignored.

At this point, it should be easy for you to write helper functions specifically for delete links. I usually write special helper functions, such as Html.DeleteProduct or Html.DeleteQuestion. Here is a piece of code for the jQuery delete function in a sample program I'm writing:

Public static string DeleteAnswerLink (this HtmlHelper html, string linkText, Answer answer) {return html.RouteLink (linkText, "answer", new {answerId = answer.Id, action = "delete"}, new {onclick=$.post (this.href); return false; "});}

The advantage of this method is that you can make full use of the existing helper function and only need to add a minimum amount of extra content through the onclick attribute.

The above is the editor for you to share how to delete ASP.NET MVC jQuery operation, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to 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