我试图创建一个自定义的List表单元素以显示从WCF服务获取的数据。以下是控件TrimSize的ViewModel的代码。 _trimSizeManager.GetRules()返回我从WCF获取的对象列表-
public class TrimSizeViewModel : ListViewModel,IBindingSettings,ICustomViewModel
{
private ITrimSizeManager _trimSizeManager;
public TrimSizeViewModel()
{
_trimSizeManager = ServiceLocator.ServiceProvider.GetService<ITrimSizeManager>();
}
public string BindingToken { get; set; }
public bool PrefillBindingValue { get; set; }
public bool DisableonDataPrefill { get; set; }
public string HelpText { get; set; }
public string FieldAPIName { get; set; }
public string DataParserClass => "ChoiceList";
protected override void InitItemProperties(Item item)
{
base.InitItemProperties(item);
this.InitBindingSettingsProperties(item);
this.InitCustomViewModelProperties(item);
Items.AddRange(_trimSizeManager.GetRules());
}
protected override void UpdateItemFields(Item item)
{
base.UpdateItemFields(item);
this.UpdateBindingSettingsFields(item);
this.UpdateCustomViewModelFields(item);
}
}
当我尝试在Sitecore表单部分的页面上加载该控件时,出现此错误stacktrace-
[NullReferenceException: Object reference not set to an instance of an object.]
ProjectABC.Forms.Fields.TrimSize.TrimSizeViewModel.InitItemProperties(Item item) in C:\codebase\ProjectABC\ProjectABC\Fields\TrimSize\TrimSizeViewModel.cs:29
Sitecore.ExperienceForms.Mvc.Pipelines.GetModel.CreateModel.Process(GetModelEventArgs args) +301
(Object,Object ) +13
Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args) +483
Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName,PipelineArgs args,String pipelineDomain,Boolean failIfNotExists) +235
Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName,String pipelineDomain) +21
Sitecore.Mvc.Pipelines.Pipelineservice.RunPipeline(String pipelineName,TArgs args) +194
Sitecore.Mvc.Pipelines.Pipelineservice.RunPipeline(String pipelineName,TArgs args,Func`2 resultGetter) +160
Sitecore.ExperienceForms.Client.Controllers.FormFieldController.RenderField(Guid fieldTypeId,Guid templateId) +625
lambda_method(Closure,ControllerBase,Object[] ) +248
system.web.Mvc.ReflectedactionDescriptor.Execute(ControllerContext controllerContext,IDictionary`2 parameters) +228
system.web.Mvc.ControlleractionInvoker.Invokeactionmethod(ControllerContext controllerContext,actionDescriptor actionDescriptor,IDictionary`2 parameters) +34
system.web.Mvc.Async.AsyncControlleractionInvoker.<BeginInvokeSynchronousactionmethod>b__39(IAsyncResult asyncResult,actioninvocation innerInvokeState) +38
system.web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +76
system.web.Mvc.Async.AsyncControlleractionInvoker.EndInvokeactionmethod(IAsyncResult asyncResult) +41
system.web.Mvc.Async.AsyncinvocationWithFilters.<InvokeactionmethodFilterAsynchronouslyRecursive>b__3d() +71
system.web.Mvc.Async.<>c__DisplayClass46.<InvokeactionmethodFilterAsynchronouslyRecursive>b__3f() +387
system.web.Mvc.Async.<>c__DisplayClass46.<InvokeactionmethodFilterAsynchronouslyRecursive>b__3f() +387
system.web.Mvc.Async.<>c__DisplayClass46.<InvokeactionmethodFilterAsynchronouslyRecursive>b__3f() +387
system.web.Mvc.Async.AsyncControlleractionInvoker.EndInvokeactionmethodWithFilters(IAsyncResult asyncResult) +42
system.web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeaction>b__1c() +38
system.web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeaction>b__1e(IAsyncResult asyncResult) +188
system.web.Mvc.Async.AsyncControlleractionInvoker.EndInvokeaction(IAsyncResult asyncResult) +38
system.web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult,ExecuteCoreState innerState) +29
system.web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +73
system.web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +52
system.web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +39
system.web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38
system.web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult,ProcessRequestState innerState) +43
system.web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +73
system.web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38
system.web.CallHandlerExecutionStep.system.web.HttpApplication.IExecutionStep.Execute() +648
system.web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +213
system.web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean& completedSynchronously) +131
我已经在Sitecore内容编辑器中检查了TrimSize模板,模板ID指南,表单元素,表单元素guid和master,并且在Sitecore上似乎都正确。我找不到导致此错误的原因。 如果我用FieldViewModel替换ListViewModel,并注释诸如Addrange之类的列表模型方法并更改“视图”页面,则代码有效,并且我没有此异常。