2012-08-16 73 views
1

我有一个淘汰赛JS视图模型,并希望使用外部的模板,得到一个MVC 4剃刀(CSHTML)绑定页面,以便初始页面可以在服务器上创建或通过淘汰赛的约束,我外部模板将在运行时决定。我想的模板的名称传递给这样的(/模板/ KnockoutTemplate?TEMPLATENAME =“规”)其中“规”是一个视图(radial.tmpl.cshtml)的名称的控制器和具有敲除把它在模板块。淘汰赛JS从MVC控制器

我的控制器:

public class TemplatesController : Controller 
{ 
    public TemplatesViewModel viewModel { get; set; } 
    public TemplatesController() 
    { 
     this.viewModel = new TemplatesViewModel { Heading = "Radial" }; 
    } 

    public ActionResult KnockoutTemplate(string templateName) 
    { 
     // is this right? 
     return PartialView(templateName, this.viewModel); 
    } 
} 

radial.cshtml

@model MVC4.Models.TemplatesViewModel 
    @{ 
     ViewBag.Title = "Radial Template"; 
    } 
    <div id="radialDashboardWidget" class="dashboardWidget" style="width: 100%"> 
    <h4 class="bold">@Model.Heading </h4> 
    <!-- or I can do this, I'll decide at development time --> 
    <h4 class="bold" data-bind="text:heading"></h4> 
    </div> 

主页

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

回答

2

我已经回答了这个位置:http://geekswithblogs.net/Aligned/archive/2012/08/17/knockout-js-and-external-mvc-cshtml-templates.aspx

我的控制器:

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

    public ActionResult KnockoutTemplate(string templateName) 
    { 
     return PartialView(templateName.Replace("/", string.Empty), this.viewModel); 
    } 
} 

radial.cshtml

@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 --> 
</div> 

仪表盘淘汰赛

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

不知道你想要达到的目标。在你CSHTML你对服务器端模型对象,而不是一个客户端JavaScript对象的观察特性“结合”。你没有在那里使用Knockout。请张贴它获取的窗口小部件对象,所以我们可以帮你的JavaScript。

+0

感谢页面,我编辑它使它成为现实尝试使之更加清晰。我希望能够利用这两种方法,并仍然加载模板与外部模板库KO。我有一个前进的方向,我只需要避开博客,而且我会在这里添加链接。 – Aligned 2012-08-24 18:43:27