Asp.net MVC learning diary 10 (delete jquery)

Source: Internet
Author: User

1. Product and productrepository are required.

Public class product
{
Public int productid {Get; set ;}
Public string name {Get; set ;}
Public double price {Get; set ;}
Public String description {Get; set ;}
Public datetime createdate {Get; set ;}
}

Public class productrepository
{
Public list <product> getproducts ()
{
List <product> result = builder <product>
. Createlistofsize (20). whereall ()
. Build ()
. Tolist ();

// Do the paging
Return result;
}

Public Product getproduct (INT productid)
{
Product =
Builder <product>. createnew (). With (x => X. productid =
Productid). Build ();
Return product;
}
}

2. Modify the index of homecontroller

Public actionresult index ()
{
Viewdata ["message"] = "Welcome to ASP. net mvc! ";
Viewdata ["Products"] = new productrepository (). getproducts ();
Return view ();
}

3. Add Delete to homecontroller

[Httppost]
Public actionresult Delete (int id)
{
// Perform Delete logic on your data
Return view ();
}

4. Index. aspx

<SCRIPT type = "text/JavaScript">
$ (Document). Ready (function (){
$ ('A. delete'). Click (function (){
VaR dodelete = confirm ('Are you sure you want to delete this record? ');
If (dodelete ){
VaR url = This. href. Replace ('Confirm ','');
VaR arr = URL. Split ('/');
VaR id = arr [arr. Length-1];
$ ('Div. busy' + id).html ("deleting ...");
$. Post (URL ,'',
Function (){
$ ('Div. iner '+ id). animate ({
Opacity: 0.25,
Left: 'toggle ',
Height: 'toggle'
},1000, function (){
$ ('Div. '+ id). Hide ();
});
});
Return false;
}
Else {return false ;}
});
});
</SCRIPT>
<Fieldset>
<Legend> Products </legend>
<% List <product> Products = viewdata ["Products"] As list <product >;%>
<% Foreach (product P in products)
{%>
<Div class = "container <% = P. productid %>">
<Div class = "busy <% = P. productid %>">
<% = Html. actionlink ("delete", "confirmdelete", new
{@ ID = P. productid}, new {@ class = "delete"}) %> </div>
<Div class = "display-label">
Productid </div>
<Div class = "display-field">
<% = Html. encode (P. productid) %> </div>
<Div class = "display-label">
Name </div>
<Div class = "display-field">
<% = Html. encode (P. Name) %> </div>
<Div class = "display-label">
Price </div>
<Div class = "display-field">
<% = Html. encode (string. Format ("{0: c}", p. Price) %> </div>
<Div class = "display-label">
Description </div>
<Div class = "display-field">
<% = Html. encode (P. Description) %> </div>
<Div class = "display-label">
Createdate </div>
<Div class = "display-field">
<% = Html. encode (string. Format ("{0: g}", p. createdate) %> </div>
<HR/>
</Div>
<% }%>
</Fieldset> <SCRIPT type = "text/JavaScript">
$ (Document). Ready (function (){
$ ('A. delete'). Click (function (){
VaR dodelete = confirm ('Are you sure you want to delete this record? ');
If (dodelete ){
VaR url = This. href. Replace ('Confirm ','');
VaR arr = URL. Split ('/');
VaR id = arr [arr. Length-1];
$ ('Div. busy' + id).html ("deleting ...");
$. Post (URL ,'',
Function (){
$ ('Div. iner '+ id). animate ({
Opacity: 0.25,
Left: 'toggle ',
Height: 'toggle'
},1000, function (){
$ ('Div. '+ id). Hide ();
});
});
Return false;
}
Else {return false ;}
});
});
</SCRIPT>
<Fieldset>
<Legend> Products </legend>
<% List <product> Products = viewdata ["Products"] As list <product >;%>
<% Foreach (product P in products)
{%>
<Div class = "container <% = P. productid %>">
<Div class = "busy <% = P. productid %>">
<% = Html. actionlink ("delete", "confirmdelete", new
{@ ID = P. productid}, new {@ class = "delete"}) %> </div>
<Div class = "display-label">
Productid </div>
<Div class = "display-field">
<% = Html. encode (P. productid) %> </div>
<Div class = "display-label">
Name </div>
<Div class = "display-field">
<% = Html. encode (P. Name) %> </div>
<Div class = "display-label">
Price </div>
<Div class = "display-field">
<% = Html. encode (string. Format ("{0: c}", p. Price) %> </div>
<Div class = "display-label">
Description </div>
<Div class = "display-field">
<% = Html. encode (P. Description) %> </div>
<Div class = "display-label">
Createdate </div>
<Div class = "display-field">
<% = Html. encode (string. Format ("{0: g}", p. createdate) %> </div>
<HR/>
</Div>
<% }%>
</Fieldset>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.