Oct 29 2009

Validation in Asp.net MVC

Create a new record in MVC (Create.aspx)

This article contain validation for

Empty values

Email validation

PhoneNo validation

[AcceptVerbs(HttpVerbs.Post)]

public ActionResult Create(tblCustomer customer)

{

if (customer.Name.Trim().Length == 0)

ModelState.AddModelError(”Name”, “First name is required.”);

if (!Regex.IsMatch(customer.PhoneNo, @”\b\d{7}”))

ModelState.AddModelError(”PhoneNo”, “Invalid phone number.”);

if (!Regex.IsMatch(customer.EmailID, @”^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$”))

ModelState.AddModelError(”EmailID”, “Invalid email address.”);

if (!ModelState.IsValid)

return View();

try

{

//TODO: Add insert logic here

customerRepository.Add(customer);

customerRepository.Save();

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

}

catch

{

return View();

}

}

To match a date in mm/dd/yyyy format, rearrange the regular expression to (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d. For dd-mm-yyyy format, use (0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d

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>

Alibi3col theme by Themocracy