Injecting content into specific sections from a partial view ASP.NET MVC 3 with Razor View Engine

后端 未结 24 1642
无人共我
无人共我 2020-11-22 06:13

I have this section defined in my _Layout.cshtml

@RenderSection(\"Scripts\", false)

I can easily use it from a view:

相关标签:
24条回答
  • 2020-11-22 06:34

    You can't need using sections in partial view.

    Include in your Partial View. It execute the function after jQuery loaded. You can alter de condition clause for your code.

    <script type="text/javascript">    
    var time = setInterval(function () {
        if (window.jQuery != undefined) {
            window.clearInterval(time);
    
            //Begin
            $(document).ready(function () {
               //....
            });
            //End
        };
    }, 10); </script>
    

    Julio Spader

    0 讨论(0)
  • 2020-11-22 06:35

    Pluto's idea in a nicer way:

    CustomWebViewPage.cs:

        public abstract class CustomWebViewPage<TModel> : WebViewPage<TModel> {
    
        public IHtmlString PartialWithScripts(string partialViewName, object model) {
            return Html.Partial(partialViewName: partialViewName, model: model, viewData: new ViewDataDictionary { ["view"] = this, ["html"] = Html });
        }
    
        public void RenderScriptsInBasePage(HelperResult scripts) {
            var parentView = ViewBag.view as WebPageBase;
            var parentHtml = ViewBag.html as HtmlHelper;
            parentView.DefineSection("scripts", () => {
                parentHtml.ViewContext.Writer.Write(scripts.ToHtmlString());
            });
        }
    }
    

    Views\web.config:

    <pages pageBaseType="Web.Helpers.CustomWebViewPage">
    

    View:

    @PartialWithScripts("_BackendSearchForm")
    

    Partial (_BackendSearchForm.cshtml):

    @{ RenderScriptsInBasePage(scripts()); }
    
    @helper scripts() {
    <script>
        //code will be rendered in a "scripts" section of the Layout page
    </script>
    }
    

    Layout page:

    @RenderSection("scripts", required: false)
    
    0 讨论(0)
  • 2020-11-22 06:35

    I have just added this code on my partial view and solved the problem, though not very clean, it works. You have to make sure the the Ids of the objects you are rendering.

    <script>
        $(document).ready(function () {
            $("#Profile_ProfileID").selectmenu({ icons: { button: 'ui-icon-circle-arrow-s' } });
            $("#TitleID_FK").selectmenu({ icons: { button: 'ui-icon-circle-arrow-s' } });
            $("#CityID_FK").selectmenu({ icons: { button: 'ui-icon-circle-arrow-s' } });
            $("#GenderID_FK").selectmenu({ icons: { button: 'ui-icon-circle-arrow-s' } });
            $("#PackageID_FK").selectmenu({ icons: { button: 'ui-icon-circle-arrow-s' } });
        });
    </script>
    
    0 讨论(0)
  • 2020-11-22 06:36

    From the solutions in this thread, I came up with the following probably overcomplicated solution that lets you delay rendering any html (scripts too) within a using block.

    USAGE

    Create the "section"

    1. Typical scenario: In a partial view, only include the block one time no matter how many times the partial view is repeated in the page:

      @using (Html.Delayed(isOnlyOne: "some unique name for this section")) {
          <script>
              someInlineScript();
          </script>
      }
      
    2. In a partial view, include the block for every time the partial is used:

      @using (Html.Delayed()) {
          <b>show me multiple times, @Model.Whatever</b>
      }
      
    3. In a partial view, only include the block once no matter how many times the partial is repeated, but later render it specifically by name when-i-call-you:

      @using (Html.Delayed("when-i-call-you", isOnlyOne: "different unique name")) {
          <b>show me once by name</b>
          <span>@Model.First().Value</span>
      }
      

    Render the "sections"

    (i.e. display the delayed section in a parent view)

    @Html.RenderDelayed(); // writes unnamed sections (#1 and #2, excluding #3)
    @Html.RenderDelayed("when-i-call-you", false); // writes the specified block, and ignore the `isOnlyOne` setting so we can dump it again
    @Html.RenderDelayed("when-i-call-you"); // render the specified block by name
    @Html.RenderDelayed("when-i-call-you"); // since it was "popped" in the last call, won't render anything due to `isOnlyOne` provided in `Html.Delayed`
    

    CODE

    public static class HtmlRenderExtensions {
    
        /// <summary>
        /// Delegate script/resource/etc injection until the end of the page
        /// <para>@via https://stackoverflow.com/a/14127332/1037948 and http://jadnb.wordpress.com/2011/02/16/rendering-scripts-from-partial-views-at-the-end-in-mvc/ </para>
        /// </summary>
        private class DelayedInjectionBlock : IDisposable {
            /// <summary>
            /// Unique internal storage key
            /// </summary>
            private const string CACHE_KEY = "DCCF8C78-2E36-4567-B0CF-FE052ACCE309"; // "DelayedInjectionBlocks";
    
            /// <summary>
            /// Internal storage identifier for remembering unique/isOnlyOne items
            /// </summary>
            private const string UNIQUE_IDENTIFIER_KEY = CACHE_KEY;
    
            /// <summary>
            /// What to use as internal storage identifier if no identifier provided (since we can't use null as key)
            /// </summary>
            private const string EMPTY_IDENTIFIER = "";
    
            /// <summary>
            /// Retrieve a context-aware list of cached output delegates from the given helper; uses the helper's context rather than singleton HttpContext.Current.Items
            /// </summary>
            /// <param name="helper">the helper from which we use the context</param>
            /// <param name="identifier">optional unique sub-identifier for a given injection block</param>
            /// <returns>list of delayed-execution callbacks to render internal content</returns>
            public static Queue<string> GetQueue(HtmlHelper helper, string identifier = null) {
                return _GetOrSet(helper, new Queue<string>(), identifier ?? EMPTY_IDENTIFIER);
            }
    
            /// <summary>
            /// Retrieve a context-aware list of cached output delegates from the given helper; uses the helper's context rather than singleton HttpContext.Current.Items
            /// </summary>
            /// <param name="helper">the helper from which we use the context</param>
            /// <param name="defaultValue">the default value to return if the cached item isn't found or isn't the expected type; can also be used to set with an arbitrary value</param>
            /// <param name="identifier">optional unique sub-identifier for a given injection block</param>
            /// <returns>list of delayed-execution callbacks to render internal content</returns>
            private static T _GetOrSet<T>(HtmlHelper helper, T defaultValue, string identifier = EMPTY_IDENTIFIER) where T : class {
                var storage = GetStorage(helper);
    
                // return the stored item, or set it if it does not exist
                return (T) (storage.ContainsKey(identifier) ? storage[identifier] : (storage[identifier] = defaultValue));
            }
    
            /// <summary>
            /// Get the storage, but if it doesn't exist or isn't the expected type, then create a new "bucket"
            /// </summary>
            /// <param name="helper"></param>
            /// <returns></returns>
            public static Dictionary<string, object> GetStorage(HtmlHelper helper) {
                var storage = helper.ViewContext.HttpContext.Items[CACHE_KEY] as Dictionary<string, object>;
                if (storage == null) helper.ViewContext.HttpContext.Items[CACHE_KEY] = (storage = new Dictionary<string, object>());
                return storage;
            }
    
    
            private readonly HtmlHelper helper;
            private readonly string identifier;
            private readonly string isOnlyOne;
    
            /// <summary>
            /// Create a new using block from the given helper (used for trapping appropriate context)
            /// </summary>
            /// <param name="helper">the helper from which we use the context</param>
            /// <param name="identifier">optional unique identifier to specify one or many injection blocks</param>
            /// <param name="isOnlyOne">extra identifier used to ensure that this item is only added once; if provided, content should only appear once in the page (i.e. only the first block called for this identifier is used)</param>
            public DelayedInjectionBlock(HtmlHelper helper, string identifier = null, string isOnlyOne = null) {
                this.helper = helper;
    
                // start a new writing context
                ((WebViewPage)this.helper.ViewDataContainer).OutputStack.Push(new StringWriter());
    
                this.identifier = identifier ?? EMPTY_IDENTIFIER;
                this.isOnlyOne = isOnlyOne;
            }
    
            /// <summary>
            /// Append the internal content to the context's cached list of output delegates
            /// </summary>
            public void Dispose() {
                // render the internal content of the injection block helper
                // make sure to pop from the stack rather than just render from the Writer
                // so it will remove it from regular rendering
                var content = ((WebViewPage)this.helper.ViewDataContainer).OutputStack;
                var renderedContent = content.Count == 0 ? string.Empty : content.Pop().ToString();
                // if we only want one, remove the existing
                var queue = GetQueue(this.helper, this.identifier);
    
                // get the index of the existing item from the alternate storage
                var existingIdentifiers = _GetOrSet(this.helper, new Dictionary<string, int>(), UNIQUE_IDENTIFIER_KEY);
    
                // only save the result if this isn't meant to be unique, or
                // if it's supposed to be unique and we haven't encountered this identifier before
                if( null == this.isOnlyOne || !existingIdentifiers.ContainsKey(this.isOnlyOne) ) {
                    // remove the new writing context we created for this block
                    // and save the output to the queue for later
                    queue.Enqueue(renderedContent);
    
                    // only remember this if supposed to
                    if(null != this.isOnlyOne) existingIdentifiers[this.isOnlyOne] = queue.Count; // save the index, so we could remove it directly (if we want to use the last instance of the block rather than the first)
                }
            }
        }
    
    
        /// <summary>
        /// <para>Start a delayed-execution block of output -- this will be rendered/printed on the next call to <see cref="RenderDelayed"/>.</para>
        /// <para>
        /// <example>
        /// Print once in "default block" (usually rendered at end via <code>@Html.RenderDelayed()</code>).  Code:
        /// <code>
        /// @using (Html.Delayed()) {
        ///     <b>show at later</b>
        ///     <span>@Model.Name</span>
        ///     etc
        /// }
        /// </code>
        /// </example>
        /// </para>
        /// <para>
        /// <example>
        /// Print once (i.e. if within a looped partial), using identified block via <code>@Html.RenderDelayed("one-time")</code>.  Code:
        /// <code>
        /// @using (Html.Delayed("one-time", isOnlyOne: "one-time")) {
        ///     <b>show me once</b>
        ///     <span>@Model.First().Value</span>
        /// }
        /// </code>
        /// </example>
        /// </para>
        /// </summary>
        /// <param name="helper">the helper from which we use the context</param>
        /// <param name="injectionBlockId">optional unique identifier to specify one or many injection blocks</param>
        /// <param name="isOnlyOne">extra identifier used to ensure that this item is only added once; if provided, content should only appear once in the page (i.e. only the first block called for this identifier is used)</param>
        /// <returns>using block to wrap delayed output</returns>
        public static IDisposable Delayed(this HtmlHelper helper, string injectionBlockId = null, string isOnlyOne = null) {
            return new DelayedInjectionBlock(helper, injectionBlockId, isOnlyOne);
        }
    
        /// <summary>
        /// Render all queued output blocks injected via <see cref="Delayed"/>.
        /// <para>
        /// <example>
        /// Print all delayed blocks using default identifier (i.e. not provided)
        /// <code>
        /// @using (Html.Delayed()) {
        ///     <b>show me later</b>
        ///     <span>@Model.Name</span>
        ///     etc
        /// }
        /// </code>
        /// -- then later --
        /// <code>
        /// @using (Html.Delayed()) {
        ///     <b>more for later</b>
        ///     etc
        /// }
        /// </code>
        /// -- then later --
        /// <code>
        /// @Html.RenderDelayed() // will print both delayed blocks
        /// </code>
        /// </example>
        /// </para>
        /// <para>
        /// <example>
        /// Allow multiple repetitions of rendered blocks, using same <code>@Html.Delayed()...</code> as before.  Code:
        /// <code>
        /// @Html.RenderDelayed(removeAfterRendering: false); /* will print */
        /// @Html.RenderDelayed() /* will print again because not removed before */
        /// </code>
        /// </example>
        /// </para>
    
        /// </summary>
        /// <param name="helper">the helper from which we use the context</param>
        /// <param name="injectionBlockId">optional unique identifier to specify one or many injection blocks</param>
        /// <param name="removeAfterRendering">only render this once</param>
        /// <returns>rendered output content</returns>
        public static MvcHtmlString RenderDelayed(this HtmlHelper helper, string injectionBlockId = null, bool removeAfterRendering = true) {
            var stack = DelayedInjectionBlock.GetQueue(helper, injectionBlockId);
    
            if( removeAfterRendering ) {
                var sb = new StringBuilder(
    #if DEBUG
                    string.Format("<!-- delayed-block: {0} -->", injectionBlockId)
    #endif
                    );
                // .count faster than .any
                while (stack.Count > 0) {
                    sb.AppendLine(stack.Dequeue());
                }
                return MvcHtmlString.Create(sb.ToString());
            } 
    
            return MvcHtmlString.Create(
    #if DEBUG
                    string.Format("<!-- delayed-block: {0} -->", injectionBlockId) + 
    #endif
                string.Join(Environment.NewLine, stack));
        }
    
    
    }
    
    0 讨论(0)
  • 2020-11-22 06:37

    Following the unobtrusive principle, it's not quite required for "_myPartial" to inject content directly into scripts section. You could add those partial view scripts into separate .js file and reference them into @scripts section from parent view.

    0 讨论(0)
  • 2020-11-22 06:40

    assume you have a partial view called _contact.cshtml, your contact can be a legal (name) or a physical subject (first name, lastname). your view should take care about what's rendered and that can be achived with javascript. so delayed rendering and JS inside view may be needed.

    the only way i think, how it can be ommitted, is when we create an unobtrusive way of handling such UI concerns.

    also note that MVC 6 will have a so called View Component, even MVC futures had some similar stuff and Telerik also supports such a thing...

    0 讨论(0)
提交回复
热议问题