我在我的li元素上执行ng-if,检查是否显示。
<li ng-if="@Model.punkt1.ToString().Length > 0">@Model.punkt1</li>
@ Model.punkt1返回一个JValue,然后我将其转换为字符串以检查长度。这是针对Umbraco中的网格编辑器,使用了razor语法。我的范围只包含字符串:
$scope.control.punkt1;
但是当我运行页面时,我收到此错误:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference
at CallSite.Target(Closure , CallSite , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at ASP._Page_app_plugins_mygrideditor_infobox_cshtml.Execute() in c:\Users\thomb\Documents\Visual Studio 2017\Projects\LoevbjergRema1000Umbraco\LoevbjergRema1000Umbraco\LoevbjergRema1000Umbraco\App_Plugins\MyGridEditor\infobox.cshtml:line 8
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection)
at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model)
at ASP._Page_Views_Partials_grid_editors_base_cshtml.Execute() in c:\Users\thomb\Documents\Visual Studio 2017\Projects\LoevbjergRema1000Umbraco\LoevbjergRema1000Umbraco\LoevbjergRema1000Umbraco\Views\Partials\Grid\Editors\Base.cshtml:line 19
为什么我无法检查字符串的长度?
编辑:
<li ng-show="@Model.punkt1 != null && @Model.punkt1.ToString().Length > 0">@Model.punkt1</li>
<li ng-show="@Model.punkt2 != null && @Model.punkt2.ToString().Length > 0">@Model.punkt2</li>
<li ng-show="@Model.punkt3 != null && @Model.punkt3.ToString().Length > 0">@Model.punkt3</li>
<li ng-show="@Model.punkt4 != null && @Model.punkt4.ToString().Length > 0">@Model.punkt4</li>
<li ng-show="@Model.punkt5 != null && @Model.punkt5.ToString().Length > 0">@Model.punkt5</li>
我的角度控制器:
angular.module("umbraco").controller("my.custom.grideditorcontroller", function ($scope) {
$scope.control.heading;
$scope.control.punkt1 = "";
$scope.control.punkt2 = "";
$scope.control.punkt3 = "";
$scope.control.punkt4 = "";
$scope.control.punkt5 = "";
});
答案 0 :(得分:0)
在将值转换为string;
试试这个
<li ng-if="@Model.punkt1 != NULL && @Model.punkt1.ToString().Length > 0">@Model.punkt1</li>
了解更多详情:Cannot perform runtime binding on a null reference - Empty Excel Cells
请与ng-show
代替ng-if
。你可以看到差异:what is the difference between ng-if and ng-show/ng-hide
您无法使用Convert.Tostring();
,因为您正在尝试获取字符串的长度。因此,如果值为null,则会抛出相同的错误。您应该参考Difference between Convert.ToString() and .ToString()的此链接。所以在这里你应该在获得我上面提到的null
值之前检查length
条件。