I work actually on asp.net core 2.2 with razor pages. In my index page, i have somme fields and a boolean field : "confirmed" with another field: "Confirmation date".
I want to display confirmation date when "confirmed" is true and to hide it otherwise. This is what i have done:
public async Task OnGetAsync()
{
Travel = await _context.Travel.ToListAsync();
}
My probleme is that i get this error :
"ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"
i know this is because there is not yet travel saved in my database, but i do not know how to make the condition. Please, any help from you is welcome.
None
0 Points
14 Posts
Razor pages hide a field depending on other field value
Sep 21, 2019 04:43 PM|marco1892|LINK
Hello everybody,
I work actually on asp.net core 2.2 with razor pages. In my index page, i have somme fields and a boolean field : "confirmed" with another field: "Confirmation date".
I want to display confirmation date when "confirmed" is true and to hide it otherwise. This is what i have done:
razor page:
<th style="@(Model.Travel[0].Confirmed==true ? "display:block" : "display:none")">
@Html.DisplayNameFor(model => model.Travel[0].ConfirmationDate)
</th>
and this is the getAsync:
public IList<Travel> Travel { get;set; }
public async Task OnGetAsync()
{
Travel = await _context.Travel.ToListAsync();
}
My probleme is that i get this error :
"ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"
i know this is because there is not yet travel saved in my database, but i do not know how to make the condition. Please, any help from you is welcome.
thank you in advance
All-Star
194434 Points
28074 Posts
Moderator
Re: Razor pages hide a field depending on other field value
Sep 21, 2019 04:56 PM|Mikesdotnetting|LINK
Initialise your list:
public IList<Travel> Travel { get;set; } = new List<Travel>();
Then you can test for .Any():
@if(Model.Travel.Any())
{
<th style="@(Model.Travel[0].Confirmed ? "display:block" : "display:none")">
@Html.DisplayNameFor(model => model.Travel[0].ConfirmationDate)
</th>
}
else
{
...
}
None
0 Points
14 Posts
Re: Razor pages hide a field depending on other field value
Sep 21, 2019 06:05 PM|marco1892|LINK
Thank you Mikes ! thats worked !
Thank you very much !