2

As going through the site i got how to access the model in javascript and how to loop it in javascript. i am using text tag to access the item in a model. when i use i am not able to add break.

        @foreach (var item in Model.ArrayDetails)
           {
               var checklower = false;
               var checkUpper = false;
               var loopentered = false;

               <text>
                if(@item.Id ==1)
               { 
                  if(@item.LowerBound <= obj.value)
                      {
                       loopentered=true;
                       checklower=true;
                     }

                  if(loopentered)
                  { 

                  alert(@item.UpperBound <= obj.value);
                     if(@item.UpperBound <= obj.value)
                        {
                     checkUpper = true;
                    }
                  }

               if(checkUpper && checklower)
               {
              ***// here i want to add break statement(if i add javascript wont work)***
               }
             }
              </text>
            }

Can some one suggest me how can solve this.

2
  • 1
    That's not Javascript. Commented Jul 5, 2013 at 14:40
  • So you want to break the server-side for loop if the client-side condition is verified? This is a bit like ordering let's say 10 items (7 + 3 spares) and wanting to receive only 7 because in the end you did not use the spares? Commented Jul 5, 2013 at 14:55

1 Answer 1

8

Don't write this soup. JSON serialize your model into a javascript variable and use this javascript variable to write your javascript code. Right now you have a terrible mixture of server side and client side code.

Here's what I mean in practice:

<script type="text/javascript">
    // Here we serialize the Model.ArrayDetails into a javascript array
    var items = @Html.Raw(Json.Encode(Model.ArrayDetails));

    // This here is PURE javascript, it could (AND IT SHOULD) go into
    // a separate javascript file containing this logic to which you could
    // simply pass the items variable
    for (var i = 0; i < items.length; i++) {
        var item = items[i];

        var checklower = false;
        var checkUpper = false;
        var loopentered = false;

        if (item.Id == 1) {

            if (item.LowerBound <= obj.value) {
                loopentered = true;
                checklower = true;
            }

            if (loopentered) { 
                alert(item.UpperBound <= obj.value);
                if(item.UpperBound <= obj.value) {
                    checkUpper = true;
                }
            }

            if (checkUpper && checklower) {
                break;
            }
        }
    }
</script>

and after moving the javascript into a separate file your view will simply become:

<script type="text/javascript">
    // Here we serialize the Model.ArrayDetails into a javascript array
    var items = @Html.Raw(Json.Encode(Model.ArrayDetails));

    myFunctionDefinedIntoASeparateJavaScriptFile(items);
</script>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.