Geeks With Blogs

News profile for Aligned at Stack Overflow, Q&A for professional and enthusiast programmers
"free in Christ Jesus from the law of sin and death." Romans 8:2 (ESV) Check out the Falling Plates video on YouTube.
more about the Gospel
And then listen to Francis Chan speaking at LifeLight in SD.

Donate Bitcoins

My Gooroo profile

Programming and Learning from SD

I can pass the name of the template to the controller like this (/Templates/KnockoutTemplate?templateName='radial') where 'radial' is the name of a view (radial.cshtml), return a partial view of that name and have Knockout put it in the template block.

My Controller:

public class TemplatesController : Controller
public TemplatesViewModel viewModel { get; set; }

public ActionResult KnockoutTemplate(string templateName, int? id)

this.viewModel.Id = id;
return  PartialView(templateName.Replace("/", string.Empty), this.viewModel);


   @model MVC4.Models.TemplatesViewModel
ViewBag.Title = "Radial Template";
<div id="radialDashboardWidget" class="dashboardWidget" style="width: 100%">
<h4 class="bold" data-bind="text:@Model.Heading">
</h4> <!-- add more HTML -->

Dashboard page with Knockout

<div id="dashboardWidgets" data-bind="foreach: Widgets" class="flexible-widget">
<!-- ko template: {name: Properties.templateName  } -->
<!-- /ko -->
<div class="clear" />

The route is:

name: "KnockoutTemplates",
url: "{controller}/{action}/{templateName}",
defaults: new { contoller = "Templates", action = "KnockoutTemplate" });


infuser.defaults.templateUrl = "/Templates/KnockoutTemplate?templateName=";
infuser.defaults.templateSuffix = "";
infuser.defaults.ajax.async = true;

Then if you want to add in the optional id, just append it to Properties.templateName before binding (properties.TemplateName + “&id=” + id).

I've asked this at StackOverflow, but didn't get an concrete responses.

Posted on Friday, August 17, 2012 8:18 AM MVC , Knockout Js | Back to top

Comments on this post: Knockout Js and External MVC cshtml templates

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Aligned | Powered by: | Join free