我遵循了这里的好建议(使用breezejs和web api处理计算的属性),以允许Breeze访问我在服务器端的部分类中设置的计算属性:
public partial class EventPerson
{
[NotMapped]
public Decimal TotalAmountPaid
{
get
{
return this.EventPersonPayments.Sum(p => p.AmtPaid);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但对于我检索的每个EventPerson,除非我使用.expand("EventPersonPayments")clientside或.Include("EventPersonPayments")serverside,否则此值显示为0.
我不希望将EventPersonPayments中的所有数据序列化并发送到客户端; 我想要的只是总和值.这可能吗?
编辑:如果我的计算属性是从实体中已有的其他属性派生的,它可以正常工作.例如:
public partial class EventPerson
{
[NotMapped]
public String DisplayName
{
get
{
return this.FirstName + " " + this.LastName;
}
}
}
Run Code Online (Sandbox Code Playgroud)
返回JSON有效内容中的DisplayName.除非我专门加载所有额外信息,否则以前计算的属性类型总是返回0或null.
我考虑将这些转换为SQL Server中的用户定义函数,但我不应该抛弃我的C#代码,只是为了让它按照应有的方式工作.
我正在尝试Knockout.js的自动完成处理程序,并且正在寻找一些反馈。目前,这是可行的,但我试图查看在没有到处放置这么多Eval()的情况下是否可以完成工作,并且为了可重用性,请查看是否有一种方法可以在不预设前提下引用ViewModel名为“ vm”,如下所示。
用法:
<input placeholder="Test..." type="search" data-bind="autoComplete:$root.persons, source:'/api/Person/', parameterName:'searchString', labelKey:'displayName', valueKey:'urid', onSelected:'addPerson'" autocomplete="off" />
Run Code Online (Sandbox Code Playgroud)
JS:
ko.bindingHandlers.autoComplete = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var postUrl = allBindingsAccessor().source; // url to post to is read here
var param = allBindingsAccessor().parameterName;
var labelKeyName = allBindingsAccessor().labelKey;
var valueKeyName = allBindingsAccessor().valueKey;
var selectedFunction = allBindingsAccessor().onSelected;
var selectedObservableArrayInViewModel = valueAccessor();
$(element).autocomplete({
minLength: 2,
autoFocus: true,
source: function (request, response) {
$.ajax({
url: param != null ? postUrl : postUrl + request.term,
data: …Run Code Online (Sandbox Code Playgroud)