Category: CRUD operations

Oct 28 2009

Delete database data using Asp.net MVC

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>

Oct 28 2009

Edit Database data using Asp.net MVC

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

Part 5: Create a new record in MVC

Details GetCustomerDetailsByID

Step a:public interface ICustomerRepository

{

tblCustomer GetCustomerByID(int id);

}

Step b: public tblCustomer GetCustomerByID(int id)

{

return db.tblCustomers.SingleOrDefault(c => c.CustomerID == id);

}

Stepc: public ActionResult Details(int id)

{

tblCustomer customer = customerRepository.GetCustomerByID(id);

return View(customer);

}

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 (Details)

Choose Create a strongly-typed view -> In view data class dropdown

choose -> myFirstMVCapp.Models.tblCustomer

View content :In dropdown choose Details

Edit Customer Details using GetCustomerById

Note: already Step a and Step b are written in Details aspx page same

Step a: Go to Model in solution Explorer -> open ICustomerRepository

add below code to retrieve all the customers

public interface ICustomerRepository

{

tblCustomer GetCustomerByID(int id);

}

Step b: Again Go to Model in Solution Explorer à open CustomerRepository.cs

Write below code

public tblCustomer GetCustomerByID(int id)

{

return db.tblCustomers.SingleOrDefault(c => c.CustomerID == id);

}

Step c: Go to Controller folder in Soution Explorer, choose

tblCustomerController.cs

Change Edit ActionResult code

//for editing data into form

public ActionResult Edit(int id)

{

tblCustomer customer = customerRepository.GetCustomerByID(id);

return View(customer);

}

//saving editing data after modification it will update the form data

[AcceptVerbs(HttpVerbs.Post)]

public ActionResult Edit(int id, FormCollection collection)

{

try

{

tblCustomer customer = customerRepository.GetCustomerByID(id);

UpdateModel(customer);

customerRepository.Save();

return RedirectToAction(”Details”, new { id = customer.CustomerID });

}

catch

{

return View();

}

}

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 (Edit)

Choose Create a strongly-typed view -> In view data class dropdown

choose -> myFirstMVCapp.Models.tblCustomer

View content :In dropdown choose Edit

Design code for Edit.aspx

<%@ Page Title=”" Language=”C#” MasterPageFile=”~/Views/Shared/Site.Master” Inherits=”System.Web.Mvc.ViewPage<myFirstMVCapp.Models.tblCustomer>” %>

<asp:Content ID=”Content1″ ContentPlaceHolderID=”TitleContent” runat=”server”>

Edit

</asp:Content>

<asp:Content ID=”Content2″ ContentPlaceHolderID=”MainContent” runat=”server”>

<h2>Edit</h2>

<%= Html.ValidationSummary(”Edit was unsuccessful. Please correct the errors and try again.”) %>

<% using (Html.BeginForm()) {%>

<fieldset>

<legend>Fields</legend>

<p>

<%–         <label for=”CustomerID”>CustomerID:</label>

<%= Html.TextBox(”CustomerID”, Model.CustomerID) %>

<%= Html.ValidationMessage(”CustomerID”, “*”) %>–%>

</p>

<p>

<label for=”Name”>Name:</label>

<%= Html.TextBox(”Name”, Model.Name) %>

<%= Html.ValidationMessage(”Name”, “*”) %>

</p>

<p>

<label for=”Address”>Address:</label>

<%= Html.TextBox(”Address”, Model.Address) %>

<%= Html.ValidationMessage(”Address”, “*”) %>

</p>

<p>

<label for=”EmailID”>EmailID:</label>

<%= Html.TextBox(”EmailID”, Model.EmailID) %>

<%= Html.ValidationMessage(”EmailID”, “*”) %>

</p>

<p>

<label for=”PhoneNo”>PhoneNo:</label>

<%= Html.TextBox(”PhoneNo”, Model.PhoneNo) %>

<%= Html.ValidationMessage(”PhoneNo”, “*”) %>

</p>

<p>

<label for=”State”>State:</label>

<%= Html.TextBox(”State”, Model.State) %>

<%= Html.ValidationMessage(”State”, “*”) %>

</p>

<p>

<label for=”Country”>Country:</label>

<%= Html.TextBox(”Country”, Model.Country) %>

<%= Html.ValidationMessage(”Country”, “*”) %>

</p>

<p>

<input type=”submit” value=”Save” />

</p>

</fieldset>

<% } %>

<div>

<%=Html.ActionLink(”Back to List”, “Index”) %>

</div>

</asp:Content>

Alibi3col theme by Themocracy