Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3753658
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:25:32+00:00 2026-05-19T09:25:32+00:00

Ok, I got this one working, but I’m not sure about how much of

  • 0

Ok, I got this one working, but I’m not sure about how much of a terrible hack this is. Can anyone tell me if what I did is ok or not, and why?

I needed to share a template between my Razor and JavaScript, so one could use it server-side and the other client-side. So, here is what I did:

Func<dynamic, HelperResult> sampleTemplate =
    @<span>Sample markup @item.Something</span>;

Then, I used my template in my View like this:

for(int idxSample = 0; idxSample < Model.Number; idxSample++) {
    @sampleTemplate(new { Something = "" })
}

And for the javascript I did this:

<script type="text/javascript">
    var someJsTemplate = "" +
        <r><[!CDATA[@sampleTemplate(new { Something = "" })]]></r>;
</script>

So I could later append someJsTemplate whenever I needed it. So, what is the verdict? Does anyone see a better way to do it? Or is this alright?

Edit:

Now I can’t use this. Although it works fine in FireFox, Chrome does not allow my hack. Any help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T09:25:32+00:00Added an answer on May 19, 2026 at 9:25 am

    Storing HTML in server side code looks like a bad idea to me. Personally I would write a controller:

    public class TemplatesController: Controller
    {
        [ChildActionOnly]
        public ActionResult Index(Item item)
        {
            return View("~/Views/Shared/EditorTemplates/Item.cshtml", item);
        }
    }
    

    and a partial (~/Views/Shared/EditorTemplates/Item.cshtml) containing the markup:

    @model AppName.Models.Item
    @{
        Layout = null;               
    }
    <span>Sample markup @Model.Something</span>
    

    Then if I need to use it in a strongly typed view I would simply use an editor template:

    @Html.EditorFor(x => x.Items)
    

    and in javascript:

    <script type="text/javascript">
        var someJsTemplate = '@HttpUtility.JavaScriptStringEncode(Html.Action("index", "templates", new { something = "abc" }).ToHtmlString())';
    </script>
    

    And to simplify a little bit this javascript call you could write a helper:

    public static class HtmlExtensions
    {
        public static MvcHtmlString Template(this HtmlHelper htmlHelper, object item)
        {
            return MvcHtmlString.Create(
                HttpUtility.JavaScriptStringEncode(
                    htmlHelper.Action("index", "templates", item).ToHtmlString()
                )
            );
        }
    }
    

    and then:

    <script type="text/javascript">
        var someJsTemplate = '@Html.Template(new { something = "abc" })';
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working on some MySQL Joins but this one has got me a
Got this line of code here but its not working. private void Button_Click(object sender,
So I've already got this working but I'm hoping someone can help me optimize
I'm really frustrated with this one. A few weeks ago I got it working
Morning y'all This is probably an easy one but I barely got any sleep
I'm really drawing a blank on this one. I've been working on globalization but
i m working in PHP since one year, but now a days i got
I'm sure I'm overlooking the obvious as I've got countless working buttons...but...for whatever reason
I've almost got this working apart from one little annoying thing... Because the ListBox
I know their are 100 posts about this one, and somehow it isn't working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.