Skip to content

Instantly share code, notes, and snippets.

@Immerseit
Created December 12, 2012 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Immerseit/4265517 to your computer and use it in GitHub Desktop.
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
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