Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @model Project.Core.Page
- @Html.MyHelper(p => new { p.Author.Name, p.Author.Location, p.Author.Age });
- public static MvcHtmlString MyHelper<TModel,TProperty>(
- this HtmlHelper<TModel> helper,
- Expression<Func<TModel,TProperty>> expression) {
- foreach (var parameter in expression.???) {
- // helper.TextBoxFor(???)
- // TagBuilder("input").Attributes("name", expression.???)
- }
- }
- public static MvcHtmlString MyHelper<TModel,object>(
- this HtmlHelper<TModel> helper,
- Expression<Func<TModel,object>> expression) {
- var newExpression = expression.Body as NewExpression;
- TModel model = helper.ViewData.Model;
- foreach (MemberExpression a in newExpression.Arguments) {
- var propertyName = a.Member.Name;
- var propertyValue = GetPropertyValue<TModel>(model, a);
- // Do whatever you need to with the property name and value;
- }
- }
- private static object GetPropertyValue<T>(T instance, MemberExpression me) {
- object target;
- if (me.Expression.NodeType == ExpressionType.Parameter) {
- // If the current MemberExpression is at the root object, set that as the target.
- target = instance;
- }
- else {
- target = GetPropertyValue<T>(instance, me.Expression as MemberExpression);
- }
- // Return the value from current MemberExpression against the current target
- return target.GetType().GetProperty(me.Member.Name).GetValue(target, null);
- }
- @Html.MyHelper(p => p.Author.Name, p => p.Author.Location, p => p.Author.Age);
- // Overload for one property
- MyHelper<TModel, TProperty1>(this ..., Expression<Func<TModel, TProperty1>> func1)
- // Overload for two properties
- MyHelper<TModel, TProperty1, TProperty2>(this ...,
- Expression<Func<TModel, TProperty1>> func1,
- Expression<Func<TModel, TProperty2>> func2)
- @(Html.MyHelper(p)
- .Add(p => p.Author.Age)
- .Add(p => p.Author.LastName, "Last Name")
- .Build())
- public static class Test
- {
- public static Helper<TModel> MyHelper<TModel>(this HtmlHelper helper, TModel model)
- {
- return new Helper<TModel>(helper, model);
- }
- }
- public class Helper<TModel>
- {
- private readonly HtmlHelper helper;
- private readonly TModel model;
- public Helper(HtmlHelper helper, TModel model)
- {
- this.helper = helper;
- this.model = model;
- }
- public Helper<TModel> Add<TProperty>(Expression<Func<TModel, TProperty>> expression)
- {
- // TODO
- return this;
- }
- public MvcHtmlString Build()
- {
- return new MvcHtmlString("TODO");
- }
- }
- @helper Print(string myvalue,string special="")
- {
- <pre> <input id="id" type="text" value ="@myvalue" data-val="@special"/> </pre>
- }
- @model IEnumerable<MvcApplication1.Models.Author>
- @{
- ViewBag.Title = "Authors";
- }
- <h2>Authors</h2>
- @foreach(var auth in Model)
- {
- @Templates.Print(auth.Name);
- }
- @model IEnumerable<MvcApplication1.Models.Book>
- @{
- ViewBag.Title = "Books";
- }
- <h2>Books</h2>
- @foreach(var book in Model)
- {
- @Templates.Print(book.Title,book.ISBN);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement