【问题标题】:Kendo and MVC style templatingKendo 和 MVC 样式模板
【发布时间】:2014-11-20 09:26:47
【问题描述】:

我试图弄清楚如何让 kendo UI 表现得像 MVC 一样。 当我渲染模板时,我想将元数据与模型一起传递,然后为给定模型生成正确的输出。

例如在 MVC 中我会这样做...

//define a custom object.cshtml in editortemplates within views
@if (ViewData.TemplateInfo.TemplateDepth < 2)
{
    foreach (var prop in ViewData.ModelMetadata.Properties
        .Where(metadata => metadata.ShowForEdit && !metadata.IsComplexType && !ViewData.TemplateInfo.Visited(metadata)))
    { 
        <li>
            @Html.Label(prop.PropertyName)
            <div class="value">
                @Html.Editor(prop.PropertyName, prop.TemplateHint, new { @class = "k-input" })
            </div>
        </li>
    }
}

//In any view after defining the above
<ul>
   @html.EditorFor(m => m)
</ul>

这会吐出一个控件列表,当编辑这些控件并将其发送回服务器时,它会映射回对象并可以保存。 这些控件将具有所有正确的属性来处理连接验证并被定义为正确的类型,因此布尔值将是复选框,而字符串将是文本框或文本区域,具体取决于元数据。

在剑道中,我想产生相同的结果,所以...... 这是我目前所拥有的:

我定义了一个这样的模板来为任意对象渲染一个编辑器......

<script id="editor" type="text/x-kendo-template">
    # var meta = getType(data.type) #
    <h2><span class="k-sprite edit"></span> #: meta.DisplayName #: <span data-bind="text:ID">#: data.ID #</span> <img src="@Url.Content("~/Content/close.png")" /></h2>
    <ul class="fieldList">
    # for(i in data) { #
        # var prop = meta.getProperty(i) #
        # if(prop != null && getTemplate(prop.Template) != null) { #
        <li>
            <label class="fieldLabel">#: prop.DisplayName  #</label>
            #= renderTemplate('editable' + prop.Template, data[prop.PropertyName]) #
        </li>
    # }} #
    </ul>
    <hr />
    <button class="k-button">Save</button>
</script>

然后我根据我在 prop.Template 中找到的值定义了单独的模板,这是 .Net 类型名称或 mvc 的 uihint 属性的值(服务器计算此值)。

在根编辑器级别模板中都很好,但是在每个字段中我都有一个模型,但不知道如何获取此处定义的变量 prop,它表示该类型对象上该属性的元数据,因此我可以确定如何正确呈现控件。

Telerik 支持建议让服务器使用 MVC 为每种类型生成一个编辑器模板,但这感觉不对,并且违背了我的目标,即让应用程序在初始加载后(大部分)只请求数据,就像 MVC 会做的那样服务器。 有什么想法吗?

【问题讨论】:

    标签: c# asp.net asp.net-mvc templates kendo-ui


    【解决方案1】:

    这永远行不通,原因:

    Javascript 和 kendo 没有类型的概念,那么元数据如何在那种环境中成为一个可行的概念。

    在这里学习的课程: 在 js 中模板化并不是 MVC 允许我们做的一半,也永远不会是由于语言的限制。

    我通过在服务器上模板化对象然后将整个对象绑定到 MVC 中 razor 生成的剑道模板来解决这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多