Created
March 2, 2009 17:25
-
-
Save brendanjerwin/72872 to your computer and use it in GitHub Desktop.
A better approach than loading associations into ViewData
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
//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); | |
}); |
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
//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