locked
How to get For Loop to exit cleanly RRS feed

  • Question

  • User248267340 posted

    Here's my razor code so far, when using a FOR loop with a List. I use this for displaying the first 4 items in a store, then I do other things

    in the view before going on.

    @for (int idx = 0; idx < 4; idx++)
     {
          var inv = Model[idx];
          ...
    }
                

    How would I modify this for when the inventory drops down to 2 or 3? I know the code in C#, but in a razor view it seems different enough.

    Any suggestions?

    Saturday, September 12, 2020 9:35 PM

Answers

  • User1312693872 posted

    Hi, coreysan

    How would I modify this for when the inventory drops down to 2 or 3?

    You can use Model.Count to get the model's count ,please check my following demo:

    View:

    @model List<MyModel>
    @for (int idx = 0; idx < Model.Count; idx++)
    {
        if (idx < 4)
        {
            var inv = Model[idx].name;
            <li>@inv</li>
        }
    }
    

    Model:

     public IActionResult Index()
            {
                var model = new List<MyModel>
                {
                    new MyModel{name = "Cloud" },
                    new MyModel{name = "Alice" },
                    new MyModel{name = "Jessie" },
                    new MyModel{name = "Jack" },
                    new MyModel{name = "Bad Guy" },
                };
                return View(model);
            }

    Result:

    Best Regards,

    Jerry Cai

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 14, 2020 3:32 AM

All replies

  • User753101303 posted

    Hi,

    My first move would be likely to use Linq Top to read at most 4 rows and assming the pther rows are  not needed at all.

    You can also use https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/break, or Math.Min. Razor views are just a convenient way to write C# code so I'm not sure what is "different enough". If rendering controls you may want to use directly Model(idx] the benefit being that the name is rendered so that the model binder will read this back as array when posted.

    Sunday, September 13, 2020 12:42 PM
  • User1312693872 posted

    Hi, coreysan

    How would I modify this for when the inventory drops down to 2 or 3?

    You can use Model.Count to get the model's count ,please check my following demo:

    View:

    @model List<MyModel>
    @for (int idx = 0; idx < Model.Count; idx++)
    {
        if (idx < 4)
        {
            var inv = Model[idx].name;
            <li>@inv</li>
        }
    }
    

    Model:

     public IActionResult Index()
            {
                var model = new List<MyModel>
                {
                    new MyModel{name = "Cloud" },
                    new MyModel{name = "Alice" },
                    new MyModel{name = "Jessie" },
                    new MyModel{name = "Jack" },
                    new MyModel{name = "Bad Guy" },
                };
                return View(model);
            }

    Result:

    Best Regards,

    Jerry Cai

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 14, 2020 3:32 AM
  • User248267340 posted

    Thanks Jerry - I'm just learning how to code inside a view, so your example really helped. Thanks!

    Monday, September 14, 2020 4:52 PM