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>

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

Leave a Reply

Alibi3col theme by Themocracy