我想要一个TimeSpan编辑器,该编辑器具有分别用于天,小时,分钟和秒的字段。 我有一个自定义编辑器tempalte,一个模型绑定器,一个模型绑定器提供程序,并注册了后者。
在呈现页面时发生NullReferenceException。
System.NullReferenceException:对象引用未设置为对象的实例。 在Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ExpressionMetadataProvider处。<> c__DisplayClass0_0 {
2.<FromLambdaExpression>g__modelAccessor|0(Object container) at Microsoft.AspNetCore.Mvc.ViewFeatures.ModelExplorer.get_Model() at Kendo.Mvc.UI.Fluent.WidgetFactory
1.NumericTextBoxFor [TValue](Expression1 expression) at Kendo.Mvc.UI.Fluent.WidgetFactory
1.IntegerTextBoxFor(Expression1 expression) at AspNetCore.Views_Shared_EditorTemplates_TimeSpanNumeric.ExecuteAsync() in E:\Source\MCT\MCT\Views\Shared\EditorTemplates\TimeSpanNumeric.cshtml:line 5 at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context) at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, Boolean invokeViewStarts) at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context) at Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.TemplateRenderer.Render() at Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.TemplateBuilder.Build() at Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper.GenerateEditor(ModelExplorer modelExplorer, String htmlFieldName, String templateName, Object additionalViewData) at Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper.Editor(String expression, String templateName, String htmlFieldName, Object additionalViewData) at Microsoft.AspNetCore.Mvc.Rendering.HtmlHelperEditorExtensions.Editor(IHtmlHelper htmlHelper, String expression, String templateName, Object additionalViewData) at Kendo.Mvc.UI.GridBoundColumn
2.GetEditor(IHtmlHelper helper ,HtmlEncoder编码器) 在Kendo.Mvc.UI.Grid1.<>c__DisplayClass69_1.<InitializeEditors>b__2(IGridBoundColumn column) at Kendo.Mvc.Extensions.EnumerableExtensions.Each[T](IEnumerable
1实例处,操作1 action) at Kendo.Mvc.UI.Grid
1.ProcessSettings() 在Kendo.Mvc.UI.WidgetBase.RenderHtml(TextWriter writer) 在Kendo.Mvc.UI.WidgetBase.ToHtmlString() 在Kendo.Mvc.UI.Fluent.WidgetBuilderBase2.WriteTo(TextWriter writer, HtmlEncoder encoder) at Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ViewBuffer.WriteToAsync(TextWriter writer, HtmlEncoder encoder) at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderLayoutAsync(ViewContext context, ViewBufferTextWriter bodyWriter) at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context) at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, String contentType, Nullable
1 statusCode处) 在Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext,IView视图,ViewDataDictionary viewData,ITempDataDictionary tempData,字符串contentType,可为空的1 statusCode) 在Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext上下文,ViewResult结果) 在Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext上下文) 在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult结果) 在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsyncTFilter,TFilterAsync 在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext上下文) 在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext [TFilter,TFilterAsync](状态和下一个,范围和范围,对象和状态,布尔值和isCompleted) 在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultFilters() 在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() 在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext上下文) 在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(状态和下一个,范围和范围,对象和状态,布尔值和已完成) 在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() 在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() 在Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext) 在Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext) 在Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext上下文) 在Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext上下文)
编辑器模板:
@model TimeSpan
@{
if (Model != null) {
@Html.Kendo().IntegerTextBoxFor(model => model.Days).HtmlAttributes(new { style = "width: 30px;" }).Spinners(false)@:.
@Html.Kendo().IntegerTextBoxFor(model => model.Hours).HtmlAttributes(new { style = "width: 30px;" }).Spinners(false).Max(23)@: :
@Html.Kendo().IntegerTextBoxFor(model => model.Minutes).HtmlAttributes(new { style = "width: 30px;" }).Spinners(false).Max(59)@: :
@Html.Kendo().IntegerTextBoxFor(model => model.Seconds).HtmlAttributes(new { style = "width: 30px;" }).Spinners(false).Max(59)
}
}
kendo ui网格呈现编辑器模板:
@(
Html.Kendo().Grid<MctDataViewModel>().Name("grid")
.ToolBar(toolbar => toolbar.Create())
.Pageable()
.Scrollable()
.Editable(e => e.Enabled(true).Mode(GridEditMode.InLine))
.Columns(c => { c.Command(cmd => { cmd.Edit().Text(" "); cmd.Destroy().Text(" "); }) ;
c.Bound(m => m.Id);
c.Bound(m => m.SequenceNumber);
c.Bound(m => m.SequenceName);
c.Bound(m => m.StepNumber);
c.Bound(m => m.StepName);
c.Bound(m => m.ParallelNumber);
c.ForeignKey(m => m.RowType, (System.Collections.IEnumerable)ViewData[nameof(MCTRowType)], "Key", "Value");
c.ForeignKey(m => m.TimeType, (System.Collections.IEnumerable)ViewData[nameof(MCTTimeType)], "Key", "Value");
c.Bound(m => m.TimeNeeded).EditorTemplateName("TimeSpanNumeric");
c.ForeignKey(m => m.InventoryType, (System.Collections.IEnumerable)ViewData[nameof(MCTInventoryType)], "Key", "Value").EditorTemplateName("GridNullableForeignKey");
c.Bound(m => m.ByInventoriesUnits);
c.Bound(m => m.ByInventoriesUnitsPerDay);
})
.DataSource(ds => ds.Ajax()
.Sort(s => { s.Add(m => m.SequenceNumber); s.Add(m => m.StepNumber); })
.Create(c => c.Action(nameof(MCT.Controllers.HomeController.CreateGridDate), "Home"))
.Read(r => r.Action(nameof(MCT.Controllers.HomeController.ReadGridData), "Home"))
.Update(u => u.Action(nameof(MCT.Controllers.HomeController.UpdateGridDate), "Home"))
.Destroy(d => d.Action(nameof(MCT.Controllers.HomeController.DeleteGridDate), "Home"))
.Model(m => {
m.Id(mct => mct.Id);
m.Field(f => f.TimeNeeded).DefaultValue(TimeSpan.Zero); })
)
)
服务配置:
public void ConfigureServices(IServiceCollection services) {
// Add framework services.
services
.AddMvc(options => {
options.ModelBinderProviders.Insert(0, new TimeSpanModelBinderProvider());
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
// Maintain property names during serialization. See:
// https://github.com/aspnet/Announcements/issues/194
.AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
// Add Kendo UI services to the services container
services.AddKendo();
}
即使告诉框架在视图模型中显式使用modelbinder,也不会改变问题。
[Microsoft.AspNetCore.Mvc.ModelBinder(typeof(TimeSpanModelBinder))]
public TimeSpan TimeNeeded { get; set; }
永远不会调用模型联编程序。从用于导航到该页面的页面上的另一个请求中调用modelbinderprovider。因此,提供者的注册似乎有效。
我想念什么?如何获得asp.net核心以呈现自定义编辑器?
亲切的问候伯恩德