0

In a c# MVC project, I was given a view by a front-end designer that contains this little script:

 <script>
    $('#PhaseDD').change(function () {
        var chosenValue = $(this).val();
        $('.ProcessDD').hide();
        if (chosenValue == "")
            $('#DefaultProcess').show();
        if (chosenValue == "Planning")
            $('#PlanningProcess').show();
        if (chosenValue == "Procurement")
            $('#ProcurementProcess').show();
        if (chosenValue == "Installation")
            $('#InstallationProcess').show();
        if (chosenValue == "Closure")
            $('#ClosureProcess').show();
    });
</script>

I would like to replace all the hard-coded options with a list that comes from the model. Something like this...

<script>
    $('#PhaseDD').change(function () {
        var chosenValue = $(this).val();
        $('.ProcessDD').hide();
        if (chosenValue == "")
            $('#DefaultProcess').show();

        // loop over a list from the model here

        if (chosenValue == " loop-item-name ")
            $('# loop-item-name + Process').show();

       //  end loop

    });
</script>

Is this possible? If so, how? And am I even going about this the right way? I was thinking I could use razor syntax, but that isn't working.

3
  • 1
    What about this: $('#' + chosenValue + 'Process').show()? You won't need a list for this. Commented Nov 4, 2016 at 14:03
  • $("#" + (chosenValue || "Default") + "Process").show(); Commented Nov 4, 2016 at 14:03
  • why are you using if else then? add Process directly to the variable Commented Nov 4, 2016 at 14:03

3 Answers 3

1

You can not compare a C# variable to a JS variable. But you can use razor to create a JS variable from a C# variable.

Here is how to fill a JS array with the values of a C# array:

@{ 
   // fetch this from ViewModel if it needs to be dynamic
   var cSharpNames = new [] { "Planning", "Procurement"}; 
 }

<script>
    var jsNames = []; // this is a JS array

    @foreach(var name in cSharpNames) {
         <text>jsNames.push(@name);</text>
    }

</script>

Then use the indexOf() method to search in the jsNames array as has been shown by Jeremy.

Sign up to request clarification or add additional context in comments.

Comments

0

I'd just make an array, check to see if the value exists within it, and if it does show it. Something like this:

<script>
    $('#PhaseDD').change(function () {
        var chosenValue = $(this).val();
        var processes = ['Planning', 'Procurement'];
        $('.ProcessDD').hide();
        if (chosenValue == "")
            $('#DefaultProcess').show();

        if (processes.indexOf(chosenValue) > -1)
            $('#' + chosenValue + 'Process').show();

    });
</script>

2 Comments

ok ok... I see what you are thinking, and this is a much better way. The syntax you have isn't working, but it might be enough to get me there.
But also, I can't have Planning, Procurment, etc, hard coded. needs to be dynamic.
0

I was overthinking this.

    $('#PhaseDD').change(function () {
        var chosenValue = $(this).val();
        $('.ProcessDD').hide();
        if (chosenValue == "")
        {
            $('#DefaultProcess').show();
        }
        else
        {
            $('#' + chosenValue + 'Process').show();
        }
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.