code_junkie

Passing data to Master Page in ASP.NET MVC

Nov 10th, 2011
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. public class MasterViewData
  2. {
  3. public ICollection<string> Navigation { get; set; }
  4. }
  5.  
  6. public class IndexViewData : MasterViewData
  7. {
  8. public string Name { get; set; }
  9. public float Price { get; set; }
  10. }
  11.  
  12. public interface IViewDataFactory
  13. {
  14. T Create<T>()
  15. where T : MasterViewData, new()
  16. }
  17.  
  18. public class ProductController : Controller
  19. {
  20. public ProductController(IViewDataFactory viewDataFactory)
  21. ...
  22.  
  23. public ActionResult Index()
  24. {
  25. var viewData = viewDataFactory.Create<ProductViewData>();
  26.  
  27. viewData.Name = "My product";
  28. viewData.Price = 9.95;
  29.  
  30. return View("Index", viewData);
  31. }
  32. }
  33.  
  34. public class IndexViewData : MasterViewData
  35. {
  36. public string Name { get; set; }
  37. public float Price { get; set; }
  38. public SubViewData SubViewData { get; set; }
  39. }
  40.  
  41. <% Html.RenderPartial("Sub", Model.SubViewData); %>
  42.  
  43. <script runat="server" type="text/C#">
  44. protected override void OnLoad(EventArgs e)
  45. {
  46. base.OnLoad(e);
  47. MasterModel = SiteMasterViewData.Get(this.Context);
  48. }
  49.  
  50. protected SiteMasterViewData MasterModel;
  51. </script>
Add Comment
Please, Sign In to add comment