2012-08-16 77 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