【问题标题】:Generate code for knockout binding for ASP.Net MVC为 ASP.Net MVC 的敲除绑定生成代码
【发布时间】:2014-05-21 08:32:00
【问题描述】:

我曾与淘汰赛工作过一点。它是一个很好的客户端数据绑定js库。在客户端绑定和填充模板如下:

<h3>Are you sure you want to delete this?</h3>
<fieldset>
    <legend>Delete</legend>
    <div class="display-label">
        Student Id
    </div>
    <div class="display-field">
        <input data-bind="value: StudentId" />

    </div>
    <div class="display-label">
        First Name
    </div>
    <div class="display-field">
        <input data-bind="value: FirstName" />
    </div>

    <div class="display-label">
        Last Name
    </div>
    <div class="display-field">
        <input data-bind="value: LastName" />

    </div>

    <div class="display-label">
        Age
    </div>
    <div class="display-field">
        <input data-bind="value: Age" />

    </div>
</fieldset>

the above way we write html to bind data

this way populate template by js code

$(function () {
    ko.applyBindings(StudentListVM);
    StudentListVM.getStudents();
});

//View Model
var StudentListVM = {
    Students: ko.observableArray([]),
    getStudents: function () {
        var self = this;
        $.ajax({
            type: "GET",
            url: '/Student/FetchStudents',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                self.Students(data); //Put the response in ObservableArray
            },
            error: function (err) {
                alert(err.status + " : " + err.statusText);
            }
        });
    },
};

self.editStudent = function (student) {
    window.location.href =  '/Student/Edit/' + student.StudentId;
};
self.deleteStudent = function (student) {
    window.location.href = '/Student/Delete/' + student.StudentId;
};

//Model
function Students(data) {
    this.StudentId = ko.observable(data.StudentId);
    this.FirstName = ko.observable(data.FirstName);
    this.LastName = ko.observable(data.LastName);
}

以上代码在客户端工作并生成 UI。

我想知道MVC中是否有任何脚手架选项,可以生成上面带有绑定表达式的html并生成所需的js视图模型。

如果这不存在,那么我将不胜感激有关如何实现它的建议。

【问题讨论】:

标签: asp.net-mvc knockout.js


【解决方案1】:
  • 看看Knockout mvc
  • 你可以写合适的@helper方法
  • 别忘了T4
  • 您可以为HtmlHelper 编写自己的扩展名

【讨论】:

  • 我也有同样的兴趣,所以我想问一下 Knockout mvc 会导致回发……是真的吗?你没有具体说明为什么需要使用辅助方法? Knockout mvc 需要额外的辅助方法吗?
  • 可以使用自定义的@helper方法根据自己的模型生成html代码
猜你喜欢
  • 2016-01-29
  • 2015-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-27
  • 1970-01-01
  • 2013-09-02
相关资源
最近更新 更多