我按照 MVC4 SPA 演练示例创建了一个简单的应用程序,在其中我下拉了 10 个主题的列表,让用户将它们标记为安全或不良,然后保存更改。现在,当用户保存更改时,我想重新加载包含接下来要处理的 10 个主题的列表。我该怎么做?
这是我的视图数据模型:
function TopicsViewModel() {
// Data
var self = this;
self.dataSource = upshot.dataSources.UnsafeTopics.refresh();
self.topics = self.dataSource.getEntities();
// Operations
self.saveAll = function () {
self.dataSource.commitChanges();
}
self.revertAll = function () {
self.dataSource.revertChanges();
}
}
看法:
@(Html.UpshotContext(bufferChanges: true).DataSource<TopicDataController>(x => x.GetUnsafeTopics()))
<button id="saveAll" data-bind="click: saveAll">Commit changes</button>
<ul data-bind="foreach: topics">
<li data-bind="css: { updated: IsUpdated }">
<strong class="topicItem" data-bind="text: TopicName"></strong>
<label><input class="bad" type="checkbox" data-bind="checked: IsBad" /> is bad</label>
<label><input class="safe" type="checkbox" data-bind="checked: IsSafe"/> is safe</label>
</li>
</ul>
<script src="~/Scripts/App/TopicsViewModel.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
ko.applyBindings(new TopicsViewModel());
});
</script>
控制器:
public class TopicDataController : DbDataController<SocialViewEntities>
{
public IQueryable<Topic> GetUnsafeTopics()
{
return DbContext.Topics.Where<Topic>(t => t.IsBad == false).Where<Topic>(t => t.IsSafe == false).Take<Topic>(10);
}
public void UpdateTopic(Topic topic) { UpdateEntity(topic); }
}