Created
December 12, 2012 06:30
-
-
Save Immerseit/4265517 to your computer and use it in GitHub Desktop.
Short and Clean Ad-Hoc View-Controller sample for quickstarting a MVC Project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainPageViewModel | |
{ | |
// a place on page | |
public string Categories { get; set; } | |
// other place on the page | |
public List<Products> Products { get; set; } | |
} | |
// Controller: | |
public class HomeController : Controller | |
{ | |
// GET: /Home/ | |
public ActionResult Index() | |
{ | |
MainPageViewModel vm = new MainPageViewModel(); | |
vm.Categories = GetCategories(); | |
vm.Products.Add(...); | |
return View(vm); | |
} | |
string[] GetCategories() | |
{ | |
DataTable data = GetDataFromQuery("SELECT * FROM Categories"); | |
} | |
string[] GetProducts() | |
{ | |
DataTable data = GetDataFromQuery("SELECT * FROM Products"); | |
} | |
DataTable GetDataFromQuery(string query) | |
{ | |
SqlDataAdapter adap = | |
new SqlDataAdapter(query, "<your connection string>"); | |
DataTable data = new DataTable(); | |
adap.Fill(data); | |
return data; | |
} | |
} | |
// Razor code | |
@model MainPageViewModel | |
@{ ViewBag.Title = "MainPage"; } | |
<div id="left-bar"> | |
<ul> | |
@foreach (var category in Model.Categories) | |
{ | |
<li>@category</li> | |
} | |
</ul> | |
</div> | |
<div id="center-content"> | |
<ul> | |
@foreach (var product in Model.Products) | |
{ | |
<li>@product.Name</li> | |
<li>@product.Price..</li> | |
} | |
</ul> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment