Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于名称映射HTML表单的JavaScript模板引擎_Javascript_Backbone.js_Asp.net Mvc 4_Template Engine - Fatal编程技术网

基于名称映射HTML表单的JavaScript模板引擎

基于名称映射HTML表单的JavaScript模板引擎,javascript,backbone.js,asp.net-mvc-4,template-engine,Javascript,Backbone.js,Asp.net Mvc 4,Template Engine,我使用主干js来处理视图和模型,但我希望在ASP.NET MVC中使用Html.EditorFor来呈现模板。这是因为我的表单是基于C#类动态创建的。我只尝试使用下划线作为JavaScript模板,但它需要像这样在值字段中标记,这对我来说不是一个选项。我需要一个模板引擎,它可以使用每个表单组件的名称映射我的表单,或者如果有其他一些视图引擎可以呈现对服务器和js模板引擎都有效的相同标记 更新 在我看来,我使用Html.EditorFor如下: @foreach (var type in Html.

我使用主干js来处理视图和模型,但我希望在ASP.NET MVC中使用Html.EditorFor来呈现模板。这是因为我的表单是基于C#类动态创建的。我只尝试使用下划线作为JavaScript模板,但它需要像这样在值字段中标记
,这对我来说不是一个选项。我需要一个模板引擎,它可以使用每个表单组件的名称映射我的表单,或者如果有其他一些视图引擎可以呈现对服务器和js模板引擎都有效的相同标记

更新

在我看来,我使用Html.EditorFor如下:

@foreach (var type in Html.GetAvailablePageModels()) {
    var content = Activator.CreateInstance(type) as IContent;
    <script id="view-template-@type.Name" type="text/html">
        @using (Html.BeginForm()) {
            @Html.EditorFor(x => content)
            <input type="submit" value="Save"/>
        }
    </script>
}
var PageModel = Backbone.Model.extend({
    urlRoot: '/api/page'
});
var page = new PageModel({ id: 'articles/85' });
page.fetch();

var EditView = Backbone.View.extend({

    el: $('div#main'),

    initialize: function () {
        this.template = _.template($('#view-template-Article').html());
        this.render();
    },

    render: function () {
        $(this.el).html(this.template(this.model.toJSON())); // <-- set the values correct in my pre rendered form
        return this;
    }

});

window.editView = new EditView({ model: page });
This is a message: {{message}}
@foreach(Html.GetAvailablePageModels()中的变量类型){
var content=Activator.CreateInstance(类型)作为IContent;
@使用(Html.BeginForm()){
@EditorFor(x=>content)
}
}
在我看来,我正在做这样的事情:

@foreach (var type in Html.GetAvailablePageModels()) {
    var content = Activator.CreateInstance(type) as IContent;
    <script id="view-template-@type.Name" type="text/html">
        @using (Html.BeginForm()) {
            @Html.EditorFor(x => content)
            <input type="submit" value="Save"/>
        }
    </script>
}
var PageModel = Backbone.Model.extend({
    urlRoot: '/api/page'
});
var page = new PageModel({ id: 'articles/85' });
page.fetch();

var EditView = Backbone.View.extend({

    el: $('div#main'),

    initialize: function () {
        this.template = _.template($('#view-template-Article').html());
        this.render();
    },

    render: function () {
        $(this.el).html(this.template(this.model.toJSON())); // <-- set the values correct in my pre rendered form
        return this;
    }

});

window.editView = new EditView({ model: page });
This is a message: {{message}}
var PageModel=Backbone.Model.extend({
urlRoot:“/api/page”
});
var page=new PageModel({id:'articles/85'});
page.fetch();
var EditView=Backbone.View.extend({
el:$('div#main'),
初始化:函数(){
this.template=..template($('.#查看模板文章').html();
这个。render();
},
渲染:函数(){

$(this.el).html(this.template(this.model.toJSON());//更改下划线模板的工作方式:


_.templateSettings = {
  interpolate : /\{\{([\s\S]+?)\}\}/g
};
现在,您可以编写如下HTML模板:

@foreach (var type in Html.GetAvailablePageModels()) {
    var content = Activator.CreateInstance(type) as IContent;
    <script id="view-template-@type.Name" type="text/html">
        @using (Html.BeginForm()) {
            @Html.EditorFor(x => content)
            <input type="submit" value="Save"/>
        }
    </script>
}
var PageModel = Backbone.Model.extend({
    urlRoot: '/api/page'
});
var page = new PageModel({ id: 'articles/85' });
page.fetch();

var EditView = Backbone.View.extend({

    el: $('div#main'),

    initialize: function () {
        this.template = _.template($('#view-template-Article').html());
        this.render();
    },

    render: function () {
        $(this.el).html(this.template(this.model.toJSON())); // <-- set the values correct in my pre rendered form
        return this;
    }

});

window.editView = new EditView({ model: page });
This is a message: {{message}}

而不是旧式的
样式。这将允许您从服务器上的Razor视图生成所需的模板。我曾经在一个项目中这样做过,效果非常好。

我可能会考虑类似于主干.Stickit的东西,因为您可能需要双向绑定到模型?这似乎很好!y是如何做到的你让你的Razor视图呈现正确的HtmlFieldName吗?如果我在视图中使用类似的东西,结果如下:但是我的JSON包含不包含内容前缀。有没有办法自动删除前缀,或者我需要手动执行此操作?老实说,我不记得了。那是一年前的事,我没有工作过w/.n从那以后…对不起,好的,我将开始一个新的问题。谢谢你的帮助!