This is a migrated thread and some comments may be shown as answers.

[Solved] Control in a Razor Partial Causes Registrars to fail

1 Answer 103 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jack E.
Top achievements
Rank 1
Jack E. asked on 17 Aug 2011, 12:25 AM
When I tried to output a control (DateTime in this case) in a Razor partial it caused the StyleSheetRegistrar and ScriptRegistrar to not emit the css nor the JS. My fix was to assign the registrar to a variable and then output it with .ToHtmlString(). This seemed to fix the problem. Any clues as to why this occurs? It seems to only be with partials and the Telerik controls work fine when places in a standard view.

The Partial (_TestPage.cshtml):
@Html.Telerik().DatePicker().Name("Test")

Calling page:
@model PerformanceTracker.Core.Announcement
 
@{
    ViewBag.Title = "Edit Announcement";
    Layout = "~/Areas/Admin/Views/Shared/_SiteLayout.cshtml";
}
<div id="breadcrumb">
    @Html.ActionLink("Dashboard", "Index", "Admin") >
    @Html.ActionLink("Announcements", "Index", "Announcement") > Announcement Edit
</div>
<h2>Edit</h2>
 
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Announcement</legend>
 
@*      @Html.Partial("_CreateOrEdit", Model)*@
        @Html.Partial("_TestPage")
 
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}
 
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

_SiteLayout.cshtml:
@using Microsoft.Web.Mvc
@using PerformanceTracker.Web.Controllers
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site2.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/Admin/admin.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/jquery-ui-1.8.15.custom.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.15/jquery-ui.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.jqia.select.js")" type="text/javascript"></script>
    @{
        // The following is to make control rendering in partials work
        var ssr = Html.Telerik().StyleSheetRegistrar()
                     .DefaultGroup(group => group
                               .DefaultPath("~/Content/Telerik")
                               .Add("telerik.common.css")
                               .Add("telerik.simple.css")
                               );
        @Html.Raw(ssr.ToHtmlString());
    }
</head>
<body>
    <div id="sfuser">
        <nobr>
            <strong>Welcome: @(User.Identity.Name)</strong> | @(Html.Raw(Html.ActionLinkForAreas<HomeController>(a => a.Story(), "Starfish Story"))) | <a href="#">Help</a> | @(Html.Raw(Html.ActionLinkForAreas<AuthController>(a => a.LogOff(), "Sign out")))
        </nobr>
    </div>
    @(Html.Raw(Html.ActionLinkForAreas<HomeController>(c => c.Index(), Html.Image("~/Content/Images/logo4_small.png", "logo", new { id = "logo" }).ToString())))
    <a id="wsdotLogo" target="_blank" href="http://www.wsdot.wa.gov"></a>
    <div id="main-container">
        <div id="main">
            <div id="content-header">
            </div>
            <div id="content-container">
                <div id="content">
                    <div id="action-bar">
                        @Html.Partial("~/Areas/Admin/Views/Shared/_adminnavmenu.cshtml")
                    </div>
                    @RenderBody()
                </div>
            </div>
        </div>
    </div>
    <!--Scripts here-->
    @{
        // The following is to make control rendering in partials work
        var sr = Html.Telerik().ScriptRegistrar().DefaultGroup(group => group.Add("telerik.common.min.js")).jQuery(false);
        @Html.Raw(sr.ToHtmlString());
    }
</body>
</html>


1 Answer, 1 is accepted

Sort by
0
qbantek
Top achievements
Rank 1
answered on 15 Nov 2011, 09:47 PM
Please check: http://www.telerik.com/community/forums/aspnet-mvc/general/issue-with-telerik-mvc3-razor-content-appearing-at-the-top.aspx#1455340

I was having the same problem and that post helped me to find the solution.
Tags
General Discussions
Asked by
Jack E.
Top achievements
Rank 1
Answers by
qbantek
Top achievements
Rank 1
Share this question
or