0

I pass null to RepresentativeTypesList viewbag but I'm trying to understand why the line

if ('@representativeTypesList' != null)

is not evaluated correctly and the cursor runs straight to the Html.Raw part of the view below. I believe I have put the @ escape chars correctly. Any help would be very much appreciated.

@{
    var representativeTypesList = @ViewBag.RepresentativeTypesList;
}

@section Header {

    <script type="text/javascript">

        if ('@representativeTypesList' != null)            
        {
            var array1 = '@Html.Raw(
                Json.Encode(
                    ((IEnumerable<RepresentativeTypesModelView>)ViewBag.RepresentativeTypesList).Select(item => new
                    {
                        itemValue = (item.Value == null ? "" : item.Value),
                        itemText = (item.Name == null ? "" : item.Name)
                    })
                )
            )';
            hdninput1 = $("#txtSalesReps_hdn");
            dropdown1 = $("#selSalesReps");
            dropdown1.append($("<option />").val('default').text('-- '@lbls.lblSelectOption' --'));
            if (array1.length > 0)
            {
                $.each(array1, function() {
                    dropdown1.append($("<option />").val(this.itemValue).text(this.itemText));
                });
            }
        }
    </script>
}
Technivorous
  • 1,682
  • 2
  • 16
  • 22
MikeG001
  • 53
  • 4
  • 1
    `if (@representativeTypesList != null)` – Shyju Nov 05 '18 at 17:36
  • You are comparing a string ''@representativeTypesList' with null so definitely it will go to the html raw directly, try to make the condition like this ''@representativeTypesList' !=='null' – Amro Mustafa Nov 05 '18 at 19:14
  • Hi, I'm sorry but representativeTypesList holds object ModelView, not a string. – MikeG001 Nov 05 '18 at 22:55

5 Answers5

0

Potentially remove the tick marks on both sides of @representativeTypesList, could be that it's evaluating the string as being not null instead of evaluating the variable as being null.

Giollia
  • 77
  • 4
0

Razor is only used to create the initial markup. Once the page is rendered (i.e., once you are running javascript in the client, instead of preparing it on the server), '@representativeTypesList' will be seen as a hardcoded string as it existed when it was rendered, it won't be seen as a javascript variable.

Instead, why not declare a javascript variable and set its initial value using Razor:

var repTypesList = @representativeTypesList;
if (repTypesList !== null)  {
    ...
}

Then you can change the value at runtime, etc.

Dave Smash
  • 2,941
  • 1
  • 18
  • 38
  • Hi, I actually tried this before but no joy. Even if I put a ViewData in the controller and set it to a different value to the one in that if statement, the cursor skips it entirely. Very weird. – MikeG001 Nov 05 '18 at 17:56
0

Can you simply pass the ViewBag object into the code without casting it to a variable first? That should work unless there is a specific reason you are needing to do this. As Elemental Pete stated Razor is used only to create the initial mark up. Razor is NEVER sent to the browser. It is processed on the server. It then renders whatever it is told to render in the HTML,CSS,JS code and that is then sent to the browser. The browser then runs the JS code. Think of your code that way and I would try replacing

if ('@representativeTypesList' != null)

with

if (@ViewBag.RepresentativeTypesList != null)

Hope that helps!

Jason

  • Hi, I notice if I put this at top of the statement, it does get evaluated: @(ViewBag.RepresentativeTypesList != null ? haveEmpty = 0 : haveEmpty = 1). But if I then use haveEmpty in the if statement it gets skipped. I think the clue is the @ char outside the brackets, but then if I do that in the original code, put @if, it messes up the entire statement with errors like IEnumerable is a type which is not valid in the given context or the name RepresentativeTypesList does nto exist in the given context. – MikeG001 Nov 05 '18 at 18:10
0

The solution was given here Using Razor within JavaScript. Because my code is inside a js section, it needs to be surrounded with "text" tags. Thanks.

MikeG001
  • 53
  • 4
0

I just tried the following in my code and it worked. Compare this to your code.

In the Controller:

public ActionResult SomeMethod(int id)
{
     ViewBag.RepresentativeTypesList = null;
}

In the View:

@{
    var representativeTypesList = ViewBag.RepresentativeTypesList;
}

@section Header {
    <script type="text/javascript">

        if ('@representativeTypesList' != null)
        {
            if (array1.length > 0)
            {
                $.each(array1, function() {
                    dropdown1.append($("<option />").val(this.itemValue).text(this.itemText));
                });
            }
        }
    </script>
}

I had to modify the code in the if statement as I did not have some of the objects that you had but when I ran this it jumped passed the if statement and did not run any of it.

Let me know if you have any questions!

Jason

Also if this or any solutions to questions you have on here works or help out please make sure you mark the answer as the correct one if it is your questions or if not but the answer is beneficial please click the up arrow to indicate that it was useful. Thank You! :)