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

后端 未结 24 1645
无人共我
无人共我 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:59

    You can use these Extension Methods: (Save as PartialWithScript.cs)

    namespace System.Web.Mvc.Html
    {
        public static class PartialWithScript
        {
            public static void RenderPartialWithScript(this HtmlHelper htmlHelper, string partialViewName)
            {
                if (htmlHelper.ViewBag.ScriptPartials == null)
                {
                    htmlHelper.ViewBag.ScriptPartials = new List<string>();
                }
    
                if (!htmlHelper.ViewBag.ScriptPartials.Contains(partialViewName))
                {
                    htmlHelper.ViewBag.ScriptPartials.Add(partialViewName);
                }
    
                htmlHelper.ViewBag.ScriptPartialHtml = true;
                htmlHelper.RenderPartial(partialViewName);
            }
    
            public static void RenderPartialScripts(this HtmlHelper htmlHelper)
            {
                if (htmlHelper.ViewBag.ScriptPartials != null)
                {
                    htmlHelper.ViewBag.ScriptPartialHtml = false;
                    foreach (string partial in htmlHelper.ViewBag.ScriptPartials)
                    {
                        htmlHelper.RenderPartial(partial);
                    }
                }
            }
        }
    }
    

    Use like this:

    Example partial: (_MyPartial.cshtml) Put the html in the if, and the js in the else.

    @if (ViewBag.ScriptPartialHtml ?? true)
        <p>I has htmls</p>
    }
    else {
        <script type="text/javascript">
            alert('I has javascripts');
        </script>
    }
    

    In your _Layout.cshtml, or wherever you want the scripts from the partials on to be rendered, put the following (once): It will render only the javascript of all partials on the current page at this location.

    @{ Html.RenderPartialScripts(); }
    

    Then to use your partial, simply do this: It will render only the html at this location.

    @{Html.RenderPartialWithScript("~/Views/MyController/_MyPartial.cshtml");}
    
    0 讨论(0)
  • 2020-11-22 06:59

    Well, I guess the other posters have provided you with a means to directly include an @section within your partial (by using 3rd party html helpers).

    But, I reckon that, if your script is tightly coupled to your partial, just put your javascript directly inside an inline <script> tag within your partial and be done with it (just be careful of script duplication if you intend on using the partial more than once in a single view);

    0 讨论(0)
  • 2020-11-22 07:00

    The first solution I can think of, is to use ViewBag to store the values that must be rendered.

    Onestly I never tried if this work from a partial view, but it should imo.

    0 讨论(0)
  • 2020-11-22 07:00

    There is a way to insert sections in partial views, though it's not pretty. You need to have access to two variables from the parent View. Since part of your partial view's very purpose is to create that section, it makes sense to require these variables.

    Here's what it looks like to insert a section in the partial view:

    @model KeyValuePair<WebPageBase, HtmlHelper>
    @{
        Model.Key.DefineSection("SectionNameGoesHere", () =>
        {
            Model.Value.ViewContext.Writer.Write("Test");
        });
    }
    

    And in the page inserting the partial view...

    @Html.Partial(new KeyValuePair<WebPageBase, HtmlHelper>(this, Html))
    

    You can also use this technique to define the contents of a section programmatically in any class.

    Enjoy!

    0 讨论(0)
  • 2020-11-22 07:00

    Using Mvc Core you can create a tidy TagHelper scripts as seen below. This could easily be morphed into a section tag where you give it a name as well (or the name is taken from the derived type). Note that dependency injection needs to be setup for IHttpContextAccessor.

    When adding scripts (e.g. in a partial)

    <scripts>
        <script type="text/javascript">
            //anything here
        </script>
    </scripts>
    

    When outputting the scripts (e.g. in a layout file)

    <scripts render="true"></scripts>
    

    Code

    public class ScriptsTagHelper : TagHelper
        {
            private static readonly object ITEMSKEY = new Object();
    
            private IDictionary<object, object> _items => _httpContextAccessor?.HttpContext?.Items;
    
            private IHttpContextAccessor _httpContextAccessor;
    
            public ScriptsTagHelper(IHttpContextAccessor httpContextAccessor)
            {
                _httpContextAccessor = httpContextAccessor;
            }
    
            public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
            {
                var attribute = (TagHelperAttribute)null;
                context.AllAttributes.TryGetAttribute("render",out attribute);
    
                var render = false;
    
                if(attribute != null)
                {
                    render = Convert.ToBoolean(attribute.Value.ToString());
                }
    
                if (render)
                {
                    if (_items.ContainsKey(ITEMSKEY))
                    {
                        var scripts = _items[ITEMSKEY] as List<HtmlString>;
    
                        var content = String.Concat(scripts);
    
                        output.Content.SetHtmlContent(content);
                    }
                }
                else
                {
                    List<HtmlString> list = null;
    
                    if (!_items.ContainsKey(ITEMSKEY))
                    {
                        list = new List<HtmlString>();
                        _items[ITEMSKEY] = list;
                    }
    
                    list = _items[ITEMSKEY] as List<HtmlString>;
    
                    var content = await output.GetChildContentAsync();
    
                    list.Add(new HtmlString(content.GetContent()));
                }
            }
        }
    
    0 讨论(0)
  • 2020-11-22 07:01

    There is a fundamental flaw in the way we think about web, especially when using MVC. The flaw is that JavaScript is somehow the view's responsibility. A view is a view, JavaScript (behavioral or otherwise) is JavaScript. In Silverlight and WPF's MVVM pattern we we're faced with "view first" or "model first". In MVC we should always try to reason from the model's standpoint and JavaScript is a part of this model in many ways.

    I would suggest using the AMD pattern (I myself like RequireJS). Seperate your JavaScript in modules, define your functionality and hook into your html from JavaScript instead of relying on a view to load the JavaScript. This will clean up your code, seperate your concerns and make life easier all in one fell swoop.

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