Part1: start MVC application
Part2: How to create Linq to sql classess
Part3: How to create Repository Pattern in Model
Part4: Display data using MVC (Details.aspx)
Part 5: Create a new record in MVC (Create.aspx)
part 6: Edit record in MVC (Edit.aspx)
Delete The Customer
Step a: Go to Model in solution Explorer -> open ICustomerRepository
add below code to retrieve all the customers
public interface ICustomerRepository
{
void Delete(tblCustomer customer)
}
Step b: Again Go to Model in Solution Explorer à open CustomerRepository.cs
Write below code
public void Delete(tblCustomer customer)
{
db.tblCustomers.DeleteOnSubmit(customer);
}
Step c: Go to Controller folder in Soution Explorer, choose
tblCustomerController.cs
Change Delete ActionResult code
//for delete Confiramation Delete.aspx
public ActionResult Delete(int id)
{
tblCustomer customer = customerRepository.GetCustomerByID(id);
return View(customer);
}
//for complete Delete customer record Deleted.aspx page. If you press //Delete button the record will be deleted completely .
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Delete(int id, string confirmButton)
{
tblCustomer customer = customerRepository.GetCustomerByID(id);
if (customer == null)
return View(”NotFound”);
customerRepository.Delete(customer);
customerRepository.Save();
return View(”Deleted”); // view folder Deleted.aspx page
}
Step d: Go to View Folder in Solution Explorer ->choose tblCustomers ->right click on tblCustomers Add New Item -> choose view -> then Add view dialog box Popups ->
View Name (Delete)
Choose Create a strongly-typed view -> In view data class dropdown
choose -> myFirstMVCapp.Models.tblCustomer
View content :In dropdown choose Empty
Note: For confiramation Create another page Deleted.aspx its view content in dropdown choose Empty
Design code
1) First Give the Delete link in Details.aspx page
<p> <%=Html.ActionLink(”Edit”, “Edit“, new {id=Model.CustomerID }) %> | <%=Html.ActionLink(”Back to List”, “Index”) %> |
<%–add below delete link button –%>
<%=Html.ActionLink(”Delete”,”Delete“,new{id=Model.CustomerID}) %>
</p>
Delete.aspx Page
<asp:Content ID=”Content2″ ContentPlaceHolderID=”MainContent” runat=”server”>
<h2>Are you sure you want to Delete customer</h2>
<% using (Html.BeginForm()) { %>
<input name=”confirmButton” type=”submit” value=”Delete” />
<% } %>
</asp:Content>
Deleted.aspx Page
<asp:Content ID=”Content2″ ContentPlaceHolderID=”MainContent” runat=”server”>
<h2>Customer Deleted</h2>
<div>
<p>Customer was successfully deleted.</p>
</div>
<div>
<p><a href=”/tblCustomer”>view customers</a></p>
</div>
</asp:Content>