N
N
niyaz_z2019-09-22 13:44:26
ASP.NET
niyaz_z, 2019-09-22 13:44:26

Why does books[i] become null?

I have a view for an Account controller with a link:

@helper BookList(List<App.Models.Book> books){
    for (int i = 0;  i < books.Count; i++){
         <td>@Html.ActionLink(books[i].Name, "Open", "Description",  books[i] , null)</td>
          <td>@books[i].Author</td>
    }
}

There is a controller method Description:
public string Open(List<App.Models.Book>  book)
        {           
            if(book == null)
            {
                return "Не выбрана";
            }
            return "Выбрана";
        }

Why, when clicking on the link, the method fires, but book becomes null, returns "Not selected"?
Tried like this:
<td>@Html.ActionLink(books[i].Name, "Open", "Description",  books[i] , null)</td>
<td>@Html.ActionLink(books[i].Name, "Open", "Description",  new { book = books[i] } , null)</td>

//сигнатуру метода также менял, не помогает
public string Open(Book  book)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander +, 2019-09-22
@AlexanderMi

Because Open expects you to link to the entire list of books, not to a specific book. If the whole list is needed there, pass it + the index of the desired book.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question