Click here to Skip to main content
15,905,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a database column called Template that saves this html content :
"
@section test{}
<div class="row" style="width:800px; margin:0 auto;">
    <div class="col-md-4" style="width:800px; margin:0 auto;">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
    </div>

  

</div>

<div class="row" style="width:800px; margin:0 auto;">
    <div class="col-md-4" style="width:800px; margin:0 auto;">
        <div>5</div>
        <div>6</div>
        <div>7</div>
        <div>8</div>
    </div>



</div>
"



This is the view that reads and translates this html :

@model CMSFC.Models.Page

@{

     Layout = "~/Views/Shared/_Layout.cshtml";
  
}


 @section test{
    <p>section test </p>
    }


        @{

            if (Model.Template != null)
            {
                // Response.Write(Model.Template);
                @Html.Raw(Model.Template)


            }





        }






And this is the Layout view :

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")

</head>
<body>




    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @*   @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })*@
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Index", "Index", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                    <li>@Html.ActionLink("News", "Index", "ContentHtml")</li>
                    @if (User.Identity.IsAuthenticated)
                    {
                        if (User.IsInRole("Admin") || User.IsInRole("Guest"))
                        {
                            <li>@Html.ActionLink("PageConfig", "Index", "PageConfigs")</li>
                            <li>@Html.ActionLink("Pages", "Index", "Pages")</li>


                        }
                    }


                </ul>

                @Html.Partial("_LoginPartial")
            </div>
        </div>
    </div>




 @*   <div id="test">
        @if (IsSectionDefined("test"))
        {
            @RenderSection("test")
        }

    </div>*@



    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p></p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>

</html>



Whta i want is , that the value of
@section test{
   <p>section test </p>
   }
,
so this paragraph to show inside the test section defined into the column Template content :


@section test{}
<div class="row" style="width:800px; margin:0 auto;">
    <div class="col-md-4" style="width:800px; margin:0 auto;">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
    </div>



And then , have a result like this :

section test
1
2
3
4
5
6
7
8

What I have tried:

------------------------------------------------
Posted
Updated 10-Sep-17 23:04pm

1 solution

I mostly used @section for injecting client-side technolgies but have you consider using Partial Views?

if not, you can visit here for Views and UI Rendering in ASP.NET MVC Applications.

^_^
 
Share this answer
 
Comments
ddgjgj 11-Sep-17 5:06am    
Im using partial views with different model class , and they are causing this error :

The model item passed into the dictionary is of type 'CMSFC.Models.Page', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[CMSFC.Models.PageConfig]'. RSS
Cristian Mayo 11-Sep-17 5:56am    
Oops! My bad. I assumed that you're passing a model/values on the partial. Alternatively, you can use: @Html.Partial("_PartialContent")

... you can reference on few examples on this link: https://docs.microsoft.com/en-us/aspnet/core/mvc/views/partial
ddgjgj 11-Sep-17 5:57am    
i did ,but still same.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900