Skip to content

Instantly share code, notes, and snippets.

@brendanjerwin
Created March 2, 2009 17:25
Show Gist options
  • Save brendanjerwin/72872 to your computer and use it in GitHub Desktop.
Save brendanjerwin/72872 to your computer and use it in GitHub Desktop.
A better approach than loading associations into ViewData
//Loading associations into ViewData
public ActionResult Edit(Guid id)
{
var model = Repositories.Model.GetById(id);
ViewData["Association"] = model.Association; //Gah! Magic Strings
return View(model);
}
//In the view:
Html.Grid<AssociationType>(
"Association", //Magic strings here too!
new Hash(empty => "There are no associations"),
column => {
column.For(p => p.Name);
column.For(p => p.Value);
});
//A better approach: navigate to the associations off the model
public ActionResult Edit(Guid id)
{
return View(Repositories.Model.GetById(id)); //Ahhh, much neater.
}
//In the view:
Html.Grid(Model.Association, //And we didn't even need to specify the type here. Type Inference works.
new Hash(empty => "There are no associations"),
column => {
column.For(p => p.Name);
column.For(p => p.Value);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment