Friday, June 17, 2011

ASP .NET MVC3 Application Structure with Code(Read Data))



The first time I learned Model-View-Controller(MVC) concept, it was very difficult to understand concept of it. However, by doing a real project I could clearly understand MVC concept. Here is an example that read data from database and show result using MVC concept in ASP .NET framework. I hope it is helpful for programmers who want to learn MVC concept.



Saturday, June 11, 2011

Web Application MVC Project Structure in ASP .NET


App_Data : Physical data is stored
Content : CSS, images and any other static files are saved
Controllers:
Models: Classes that represent the application model for MVC Web Application are stored
Scripts :
Views :

ASP .NET MVC3 ViewBag

A ViewBag is a dynamic object that can be used to pass data from Controllers to Views as you use ViewData[]. It enables developers can use get/set values and add additional fields without strongly-typed classes. To understand this concepts, let's compare ViewData[] and ViewBag.

- Using ViewData[]
-Controller
public ActionResult Index()
{
List<string> colors = new List<string>();
colors.Add("red");
colors.Add("green");
colors.Add("blue");

ViewData["listColors"] = colors;
ViewData["dateNow"] = DateTime.Now;
ViewData["name"] = "Hajan";
ViewData["age"] = 25;

return View();
}
-Views
<p>
My name is
<b><%: ViewData["name"] %>b>,
<b><%: ViewData["age"] %>b> years old.
<br />
I like the following colors:
p>
<ul id="colors">
<% foreach (var color in ViewData["listColors"] as List<string>){ %>
<li>
<
font color="<%: color %>"><%: color %>font>
li>
<% } %>
ul>
<p>
<%: ViewData["dateNow"] %>
p>

- Using ViewBag
-Controller
public ActionResult Index()
{
List<string> colors = new List<string>();
colors.Add("red");
colors.Add("green");
colors.Add("blue");

ViewBag.ListColors = colors; //colors is List
ViewBag.DateNow = DateTime.Now;
ViewBag.Name = "Hajan";
ViewBag.Age = 25;
return View();
}
-Views
<p>
My name is
<b><%: ViewBag.Name %>b>,
<b><%: ViewBag.Age %>b> years old.
<br />
I like the following colors:
p>
<ul id="colors">

<% foreach (var color in ViewBag.ListColors) { %>
<li>
<font color="<%: color %>"><%: color %>font>
li>
<% } %>

ul>
<p>
<%: ViewBag.DateNow %>
p>

Change the body style in ASP .NET framework




the main CSS file is int the content folder and is named Site.css. To change the body style, right-click inside the body style description in the code, and click Build Sytle on the shortcut menu