0
   @foreach ((Employee, int) result in searchResults)
   {
        <tr>
            <th scope="row">@result.Item1.Forename, @result.Item1.Surname
                <i class="bi bi-info-circle" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasRight" aria-controls="offcanvasRight"></i>
                <div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasRight" aria-labelledby="offcanvasRightLabel">
                <div class="offcanvas-header">
                    <h5 id="offcanvasRightLabel">@result.Item1.Forename, @result.Item1.Surname</h5>
                    <button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
                </div>
                <div class="offcanvas-body">
                    <Profile4Search Id = "@result.Item1.Id"/>
                </div>
                </div>
            </th>

I want to put offcanvas for Employee's information but the Profile4Search components are not rerendered and always show the same information. How can I rerender the component in foreach loop?

i thought , the component ProfileSearch receive Id from Parameter and show detail of each employees (if user clicks icon)but its rendered only once, and show same Employee's Detail(here the first Employee in loop).

  • 1
    _"are not rerendered and show always"_ is a contradiction, please clarify. – H H Jul 25 '21 at 20:58
  • [Polite] Like @HenkHolterman I have no real idea what I'm looking at here. you need to reduce this down to some structured code that demonstrates your problem. A block of code with no context is very hard to understand. – MrC aka Shaun Curtis Jul 25 '21 at 21:34
  • The edit shortened but does not clarify. Try to add expected vs actual results. – H H Jul 25 '21 at 22:14
  • Also, the foreach() loop does not close. That might matter. – H H Jul 25 '21 at 22:15
  • i thought , the component `ProfileSearch` receive Id from Parameter and show detail of each employees (if user clicks icon)but its rendered only once, and show same Employee's Detail(here the first Employee in loop)..... – 짜요짜요 Jul 25 '21 at 22:21
  • actually i closed the foreach loop and there is more code under but too long and not related.... – 짜요짜요 Jul 25 '21 at 22:24
  • It's not clear but try to replace `(Employee, int)` wit `var` and remove all the `.Item1` (so, just `@result.Id`). You never use `Item2`. – H H Jul 25 '21 at 22:40
  • Are you in Korea? I am in Korea, maybe I can help, let me know. Anyway, the whole thing I'm seeing looks like you're trying to mash together existing JavaScript DOM manipulations with Blazor. I suspect that you could achieve all your goals with much simpler and cleaner code. – Bennyboy1973 Jul 26 '21 at 09:18
  • @짜요짜요 what is the *actual* output? Post the actual HTML generated. It's unclear whether you get the same row over and over, if you want to redraw the table to display new data, or if you want to display a selected object somewhere else. What `Profile4Search` component are you talking about? Is `offcanvas` a CSS rule? A CSS rule affects how HTML *renders*. Re-render means displaying something *again* for some reason, eg after a change – Panagiotis Kanavos Jul 26 '21 at 13:15

1 Answers1

-2

When using a for-each loop in C# it's often necessary to assign the loop variable to a temporary variable.

@foreach ((Employee, int) result in searchResults)
   {
       var tmp = result.Item1;
        <tr>
            <th scope="row">@tmp.Forename, @tmp.Surname
  

Not easy to be certain that is the cause here as your question isn't quite clear on what is / is-not being rendered. More: Why is a temporary variable required in foreach to be included in lambda expression?

Quango
  • 12,338
  • 6
  • 48
  • 83
  • 1
    No, a `foreach()` doesn't need this workaround. – H H Jul 26 '21 at 10:49
  • 1
    `it's often necessary to assign the loop variable` no, it's never necessary. It may be convenient, if you need to access nested objects and properties, but never necessary. What you linked to describes issues with *lambdas*, not `foreach` – Panagiotis Kanavos Jul 26 '21 at 13:11