0

This may be a bit moot, but it is something I want to do just for the sake of seeing how, and I think it is a little cleaner than the typical method.

Right now, I can pass extra ViewData to a partial view like this;

@Html.Partial( "path/to/partial", new ViewDataDictionary{{ "key", "value" }})

And that works fine, though I was wondering, would it be possible to make an extension method that would just clean it up a bit? I've tried to do it for a few hours now and am finding myself a bit lost, but I am attempting to go the route of ..

@Html.Partial( "path/to/partial" ).With("key","data").With("key2","data2")

So basically, it's just syntax sugar. But I haven't been able to get this to work. Has anyone else achieved something similar?

Update

I actually may have solved this myself. I had to turn the idea upside down to get the result I want. I'm not sure if I'll even use it, given how obtuse it is, but it was interesting to learn about! I am very welcoming to any insight that could be used to make this better. But essentially, the trick was to do it in reverse.

using System;
namespace System.Web.Mvc.Html {
    public static partial class PartialViewExtensions {
        /// <summary>
        /// An Html Helper specifically for rendering a partial view with a fluently declared ViewDataDictionary.
        /// </summary>
        public class PartialHtmlHelper : HtmlHelper
        {
            public PartialHtmlHelper(ViewContext viewContext, IViewDataContainer viewDataContainer) 
                : base(viewContext, viewDataContainer) { }  
        }

        /// <summary>
        /// Prepare the upcoming partial view with view data.
        /// </summary>
        public static PartialHtmlHelper With( this HtmlHelper helper, string key, dynamic value ) {
            var partial = new PartialHtmlHelper( helper.ViewContext, helper.ViewDataContainer );
            partial.ViewData.Add( key, value );
            return partial;
        }

        /// <summary>
        /// Prepare the upcoming partial view with view data.
        /// </summary>
        public static PartialHtmlHelper With( this PartialHtmlHelper helper, string key, dynamic value ) {
            helper.ViewData.Add( key, value ); return helper;
        }

        /// <summary>
        /// Render a prepared partial view with readied view data.
        /// </summary>
        public static MvcHtmlString Using( this PartialHtmlHelper helper, string partialViewName ) {
            return helper.Partial( partialViewName, helper.ViewData );
        }
    }
}

And that makes it very easy to declare with fluent, chained methods.

@Html
   .With("alpha", "alpha.property" )
   .With("beta", "beta.property" )
   .With("gamma", "gamma.property")
   .With("epsilon", DateTime.UtcNow() )
   .With("sigma", "sigma.property" )
   .Using("Templates/Form/Slots")
6
  • Yeah, that is my goal. I essentially would love to declare each item in the dictionary as a chain of extensions, and then the whole thing gets rendered as if I had done it the original way. I know it sounds silly, but it is just the kind of way I like to code. If I'm going to use the helpers, I at least want them to feel fluent. Commented Sep 17, 2014 at 22:09
  • Show us the code for your With() method. Commented Sep 17, 2014 at 22:09
  • Unfortunately, I don't have any code for the With method yet. I'm still stuck trying to figure out what to attach it to. I thought attaching it to MvcHtmlString would work, but that doesn't seem to behave like I thought. Commented Sep 17, 2014 at 22:10
  • The problem is, the MvcHtmlString class hierarchy doesn't contain much functionality that will help you here (all it appears to do is give you a ToHtmlString() method). You might be better off writing your own Partial() extension method, wrapping one of the existing Partial() methods. Commented Sep 17, 2014 at 22:18
  • 1
    This answer about creating fluent html helpers may help Commented Sep 17, 2014 at 23:12

1 Answer 1

1

Hence Html.Partial is an extension method so you can't add an extension method to an extension method.

Sign up to request clarification or add additional context in comments.

1 Comment

You're right. So I just turned it upside down. I added the answer I came to up above, just now. I'm not sure if it really serves any useful purpose, but it was an interesting thing to dig into.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.