Home > Software design >  ASP.NET function wont return value from for loop
ASP.NET function wont return value from for loop

Time:07-13

Im trying to return value from function which should return Dictionary after going throught while loop. Problem is loop goes only once and when I try debug this the breakpoint at return wont it wont even fire up. No exception if fired up.

public async Task<Dictionary<TimeSpan, TimeSpan>> GetTimes(int id)
        {
            Dictionary<TimeSpan, TimeSpan> hours = new Dictionary<TimeSpan, TimeSpan>();
            var room = await GetRoom(id);
            int i = room.Open;
            while (i < room.Close)
            {
                TimeSpan from = TimeSpan.FromHours(i);
                TimeSpan to = TimeSpan.FromHours(  i);

                hours.Add(from, from);
                hours.Add(from, to);           
            }

            return hours;
        }

function it self is calling from Controler.

 public async Task<IActionResult> CreateReserve(DateTime dateTime,int id)
    {
        if(dateTime >=  DateTime.Now)
        {
            return PartialView("RoomCount", await _room.GetTimes(id));
        }

        return PartialView("Reservations");
    }

Action from controller is called from view

<input  onchange="SetTime({ value:this.value,id: @Model.Id })" type="date" value="@DateTime.Today.ToString("dd-MM-yyyy")" />

<script>function SetTime({value,id}) {
    @*$.post("/Home/CreateReserve", { dateTime: value });*@
    $("#reservations").load("/Home/CreateReserve",{ dateTime: value,id: id });
}
    </script>
    ```

CodePudding user response:

No exception if fired up.

Yes there is. Right here:

hours.Add(from, to);

Problem is loop goes only once and when I try debug this the breakpoint at return wont it wont even fire up.

Because the last line of the loop throws an exception. Control exits the method. The calling method doesn't catch exceptions, so control exits that too. The problem here is that you're not observing the result of the operation. Go back to your browser's debugging tools and see that the AJAX operation returned a result, and that result was an error. (When developing web applications, it's a good idea to use your IDE for debugging the server-side code and rely on your browser's debugging tools for the client-side code.)

So for one thing, you'll need to add some better error handling both server-side and client-side so you can observe these things.

More to the point, what's happening here is that you're adding two elements to the Dictionary<> with the same key:

hours.Add(from, from);
hours.Add(from, to);

Dictionary keys must be unique (or, more specifically, produce unique hash codes). It's not entirely clear what data structure you're trying to create here or for what purpose, but what is clear is that you can't add two elements to a Dictionary<> with the same key.

Maybe you want a list of custom objects instead? Or use some other value as the key? Something else? That's really up to you and the domain you're modeling in the application.

CodePudding user response:

You never increment/change the value of i inside your loop. If room.Open is less than room.Close, it will loop endlessly. That is probably why your breakpoint will not be hit.

  • Related