Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. @model Project.Core.Page
  2. @Html.MyHelper(p => new { p.Author.Name, p.Author.Location, p.Author.Age });
  3.  
  4. public static MvcHtmlString MyHelper<TModel,TProperty>(
  5. this HtmlHelper<TModel> helper,
  6. Expression<Func<TModel,TProperty>> expression) {
  7. foreach (var parameter in expression.???) {
  8. // helper.TextBoxFor(???)
  9. // TagBuilder("input").Attributes("name", expression.???)
  10. }
  11. }
  12.  
  13. public static MvcHtmlString MyHelper<TModel,object>(
  14. this HtmlHelper<TModel> helper,
  15. Expression<Func<TModel,object>> expression) {
  16.  
  17. var newExpression = expression.Body as NewExpression;
  18. TModel model = helper.ViewData.Model;
  19.  
  20. foreach (MemberExpression a in newExpression.Arguments) {
  21.  
  22. var propertyName = a.Member.Name;
  23. var propertyValue = GetPropertyValue<TModel>(model, a);
  24.  
  25. // Do whatever you need to with the property name and value;
  26.  
  27. }
  28.  
  29. }
  30.  
  31. private static object GetPropertyValue<T>(T instance, MemberExpression me) {
  32.  
  33. object target;
  34.  
  35. if (me.Expression.NodeType == ExpressionType.Parameter) {
  36. // If the current MemberExpression is at the root object, set that as the target.
  37. target = instance;
  38. }
  39. else {
  40. target = GetPropertyValue<T>(instance, me.Expression as MemberExpression);
  41. }
  42.  
  43. // Return the value from current MemberExpression against the current target
  44. return target.GetType().GetProperty(me.Member.Name).GetValue(target, null);
  45.  
  46. }
  47.  
  48. @Html.MyHelper(p => p.Author.Name, p => p.Author.Location, p => p.Author.Age);
  49.  
  50. // Overload for one property
  51. MyHelper<TModel, TProperty1>(this ..., Expression<Func<TModel, TProperty1>> func1)
  52.  
  53. // Overload for two properties
  54. MyHelper<TModel, TProperty1, TProperty2>(this ...,
  55. Expression<Func<TModel, TProperty1>> func1,
  56. Expression<Func<TModel, TProperty2>> func2)
  57.  
  58. @(Html.MyHelper(p)
  59. .Add(p => p.Author.Age)
  60. .Add(p => p.Author.LastName, "Last Name")
  61. .Build())
  62.  
  63. public static class Test
  64. {
  65. public static Helper<TModel> MyHelper<TModel>(this HtmlHelper helper, TModel model)
  66. {
  67. return new Helper<TModel>(helper, model);
  68. }
  69. }
  70.  
  71. public class Helper<TModel>
  72. {
  73. private readonly HtmlHelper helper;
  74. private readonly TModel model;
  75.  
  76. public Helper(HtmlHelper helper, TModel model)
  77. {
  78. this.helper = helper;
  79. this.model = model;
  80. }
  81.  
  82. public Helper<TModel> Add<TProperty>(Expression<Func<TModel, TProperty>> expression)
  83. {
  84. // TODO
  85. return this;
  86. }
  87.  
  88. public MvcHtmlString Build()
  89. {
  90. return new MvcHtmlString("TODO");
  91. }
  92. }
  93.  
  94. @helper Print(string myvalue,string special="")
  95. {
  96. <pre> <input id="id" type="text" value ="@myvalue" data-val="@special"/> </pre>
  97. }
  98.  
  99. @model IEnumerable<MvcApplication1.Models.Author>
  100. @{
  101. ViewBag.Title = "Authors";
  102. }
  103.  
  104. <h2>Authors</h2>
  105.  
  106. @foreach(var auth in Model)
  107. {
  108. @Templates.Print(auth.Name);
  109. }
  110.  
  111. @model IEnumerable<MvcApplication1.Models.Book>
  112. @{
  113. ViewBag.Title = "Books";
  114. }
  115.  
  116. <h2>Books</h2>
  117.  
  118. @foreach(var book in Model)
  119. {
  120. @Templates.Print(book.Title,book.ISBN);
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement