Wednesday, June 26, 2013

How to display and view details in MVC?

//Controller
 //Controller/CustomerController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyFirstMVC.Models;
namespace MyFirstMVC.Controllers
{
public class CustomerController : Controller
{
List<Customer> Customers = new List<Customer>();
public CustomerController()
{
var dataContext = new CustDBDataContext();
var Customers1 = (from m in dataContext.tbl1s
select m).ToList();
foreach (var c in Customers1)
{
Customer obj1 = new Customer();
obj1.Id = (int)c.Id;
obj1.Name = c.Name;
obj1.Amount = (double)c.Amount;
Customers.Add(obj1);
}
}
public ViewResult DisplayCustomer(int id)
{
List<Customer> objCustomer1 = Customers.FindAll(delegate(Customer cst) { return cst.Id == id; });
Customer objCustomer = objCustomer1[0];
return View("DisplayCustomer", objCustomer);
}
public ViewResult Details()
{

return View(Customers);
}
}
}



//Model
 //Model/Customer.cs
using System;

using System.Collections.Generic;

using System.Web;

using System.Linq;

using System.ComponentModel;

namespace MyFirstMVC.Models



{

public class Customer

{

private int _Id;

private string _Name;

private double _Amount;

public int Id

{

set

{

_Id = value;

}

get

{

return _Id;

}

}

public string Name

{

get

{

return _Name;

}

set

{

_Name = value;

}

}

public double Amount

{

set

{

_Amount = value;

}

get

{

return _Amount;

}

}



}



}


// View Display//
//Views/Customer/Details.aspx<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MyFirstMVC.Models.Customer>>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Details</title>

</head>

<body>

<h2><%: ViewData["Message"] %></h2>



<table>

<tr>

<th>


CustomerId
</th>

<th>


CustomerName
</th>

<th>


CustomerAddress
</th>

<th>


CustomerMobileNo
</th>

<th>


CustomerCountry
</th>

<th></th>

</tr>

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

<% foreach (var item in Model)

{%>

<tr>

<td>

<%=item.Id%>





</td>

<td>

<%=item.Name%>



</td>

<td>

<%=item.Amount%>



</td>

<td>

<%=Html.ActionLink("Details", "DisplayCustomer", new { id = item.Id })%>





</td>



</tr>

<%}

%>

<% }

%>



</table>



</body>

</html>
//Details//
////Views/Customer//DisplayCustomer.aspx

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MyFirstMVC.Models.Customer>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>DisplayCustomer</title>

</head>

<body>

<div>

The customer id is <%= Model.Id %> <br />

The customer Code is <%= Model.Name %> <br />

<% if (Model.Amount > 100) {%>



This is a priveleged customer
 
<% } else{ %>



This is a normal customer

<%} %>
 
</div>

</body>

</html>


Enter Url//../Customer/Details
 

No comments:

Post a Comment