0

on post I have some error checking before saving and if true I return to Page()

In debugging I found out that it is a problem with my Drop Down Selection

    <div class="form-group">
        <label asp-for="WorkColUser.WorkColRoleId" class="control-label"></label>
    </div>
    <select id="RoleId" asp-for="RoleDropDownDisp" asp-items="@(new SelectList(Model.RoleDropDownDisp,"RoleId","RoleName"))">
        <option value="" selected disabled>@Localizer["--Choose Role--"]</option>
    </select>
    <div class="form-group">
        <input asp-for="WorkColUser.WorkColRoleId" readonly="readonly" class="form-control" />
        <span asp-validation-for="WorkColUser.WorkColRoleId" class="text-danger"></span>
    </div>

The problem is only if I have no error. Otherwise all is working fine.

my RoleDropDownDisp is defined as follows:

public IEnumerable<Role> RoleDropDownDisp { get; set; }

What can I do to avoid this error? Thanks for helping

1 Answers1

0

I think you better take care of null case in your model but still a quick fix may be:

@(new SelectList(Model?.RoleDropDownDisp ?? new List<Role>(),"RoleId","RoleName"))
ShayD
  • 689
  • 7
  • 17
  • Thank you it is not crashing anymore. However now my dropdown are empty, which looks not nice. How can I reload my dropdown. Can you explain me why the RoleDropDownDisp is Null after returning? is the IEnumerable RoleDropDownDisp { get; set; } not kept? Tks for helping – Christof Oberholzer Mar 15 '21 at 20:17
  • When does it happens? When posting a form? can you add details? – ShayD Mar 15 '21 at 20:48
  • I found out. Thanks for helping!! In posting the model my List was deleted. So I had in the Post process to add the read of my list again. RoleDropDownDisp = await _context.Role.ToListAsync(); // Added for I did not expect that in post that I would loos my list. Anyway you helped a lot!! Thanks – Christof Oberholzer Mar 15 '21 at 20:55