1

I'm performing an ng-if on my li element, to check whether to show it or not.

<li ng-if="@Model.punkt1.ToString().Length > 0">@Model.punkt1</li>

@Model.punkt1 returns a JValue, which i then convert to a string to check the length. This is for a grid editor in Umbraco, razor syntax is used. My scope merely contains the strings:

$scope.control.punkt1;

But I am getting this error when i run the page:

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

Why am i not able to check the length of my string?

EDIT:

<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>

And my angular controller:

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 = "";
});
Leth
  • 1,033
  • 3
  • 15
  • 40
  • Possible duplicate of [Cannot perform runtime binding on a null reference - Empty Excel Cells](https://stackoverflow.com/questions/28235162/cannot-perform-runtime-binding-on-a-null-reference-empty-excel-cells) – Ramesh Rajendran Sep 01 '17 at 06:55
  • so, you're getting a javscript variable name in the model? – adiga Sep 01 '17 at 07:08
  • I am getting a response back in JSON that has these properties: http://i.imgur.com/BT8KN8E.png , where "value" is my "punkt1" variable. – Leth Sep 01 '17 at 07:10
  • what is the datatype of `punkt1` – adiga Sep 01 '17 at 07:17
  • As I understand it, it is a JValue datatype. – Leth Sep 01 '17 at 07:18

1 Answers1

0

Check the null condition before convert the value to string;

Try this

<li ng-if="@Model.punkt1 != NULL &&  @Model.punkt1.ToString().Length > 0">@Model.punkt1</li>

for more details : Cannot perform runtime binding on a null reference - Empty Excel Cells

and kindly go with ng-show instead of ng-if. you can see the difference : what is the difference between ng-if and ng-show/ng-hide

EDIT:

You can't use Convert.Tostring(); because of you are trying to get the length of string. So if the values is null , then the same error will threw. You should refer this link for Difference between Convert.ToString() and .ToString(). So here you should check a null condition before getting the length of value which I mentioned above.

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • I am getting a different error now.I have included it as an edit. – Leth Sep 01 '17 at 06:59
  • I tried the solution you linked, but no reference for "Convert" was found. I will try using ng-show instead. – Leth Sep 01 '17 at 07:04
  • Thanks, i have made an edit with my current code, but am still getting a null reference error. – Leth Sep 01 '17 at 07:14