【发布时间】: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视图模型。
如果这不存在,那么我将不胜感激有关如何实现它的建议。
【问题讨论】:
-
我找到了这样的visualstudiogallery.msdn.microsoft.com/…,但仍在寻找更多出路。谢谢
-
如果你找到了,为什么还要寻找更多的方法?此外,很难“给”某人轻松做某事的知识。如果您想了解更多关于 ASP.NET MVC 中的脚手架,我建议您查看 T4 模板。有一个关于 SO 上的自定义视图模板的问题,它收到了接受的答案,stackoverflow.com/questions/1009067/…。您可以在msdn.microsoft.com/en-us/library/bb126445.aspx 阅读有关 T4 的更多信息以及左侧列表中该部分的子页面,以及大量内容。