Here I am able to bind the multi-select select list on the create page.
<select id="multiple" asp-for="SecurityLog.Officer" multiple="multiple" class="selectpicker form-control" asp-items="ViewBag.Officer">
</select>
public IActionResult OnGetAsync()
{
ViewData["Officer"] = new SelectList(_context.Officer.Where(a => a.Active == "Y"), "ID", "FullName");
return Page();
}
The biggest problem I'm having is that I am trying to edit/post values and there's no direct field to bind to in my model.
I have found a good example here, but it only shows you how to save 1 selection. https://www.learnrazorpages.com/razor-pages/forms/select-lists
My SecurityLog Model has this
public virtual Officer Officer { get; set; }
My Officer Model has this
namespace SecurityCore.Models
{
public class Officer
{
[Required]
public int ID { get; set; }
[Required]
[Display(Name = "Officer's First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Officer's Last Name")]
public string LastName { get; set; }
[Display(Name = "Officer's Name")]
public string FullName { get; set; }
[Required]
public string Active { get; set; }
}
}
I do not have a SecurityLogOfficer Model. Do I need one?
And here is my database schema and sample results
Any assistance would be appreciated!
* UPDATE *
Thank you for your suggestion to bind to a list and change the asp-for to that list. Here is my updated code. SelectedOfficerIds does store the selected id's.
<select id="multiple" asp-for="SelectedOfficerIds" multiple="multiple" class="selectpicker form-control" asp-items="ViewBag.Officer">
</select>
I am posting to two different databases (1 record should go to SecurityLog, and multiple to SecurityLogOfficer), but since I am calling savechanes() twice it is actually posting multiple records to the SecurityLog db and on the second for loop save I receive an error
Cannot insert explicit value for identity column in table 'SecurityLogOfficer' when IDENTITY_INSERT is set to OFF
_context.SecurityLog.Add(SecurityLog);
//We need to ensure we have the ID of the newly posted event
_context.SaveChanges();
int tempSecurityLogID = SecurityLog.ID;
for (int i = 0; i < SelectedOfficerIds.Length; i++)
{
SecurityLogOfficer.SecurityLogID = tempSecurityLogID;
SecurityLog = null;
SecurityLogOfficer.OfficerID = SelectedOfficerIds[i];
_context.SecurityLogOfficer.Add(SecurityLogOfficer);
await _context.SaveChangesAsync();
}
Message = "Entry added successfully!";
Is there a way to clear out _context for SecurityLog so I only post once and how can I address the identity_insert issue? The db, SecurityLogOfficer, already has identity insert on.
Update 1/9/2020
@XingZou - Thank you for the quick reply. I attempted to implement your steps and everything worked out perfectly!!!
I do have one additional question if you don't mind? I am attempting to get a list of the selected values on the Edit Page and cannot set the values. This is what I tried so far...
Here is my Edit Page Model...
[BindProperty]
public MultiSelectList GetExistingSelectedOfficerIds { get; set; }
public async Task<IActionResult> OnGetAsync(int? id)
{
var i = _context.SecurityLogOfficer.Where(a => a.SecurityLogID == SecurityLog.ID);
SecurityLogOfficers.AddRange(i);
int[] GetExistingSelectedOfficerIds = new int[SecurityLogOfficers.Count];
int itemCount = 0;
foreach (var item in SecurityLogOfficers)
{
GetExistingSelectedOfficerIds[itemCount] = item.OfficerID;
itemCount++;
}
return Page();
}
Here is my Edit razor page...
<select id="multiple" asp-for= "GetExistingSelectedOfficerIds" multiple="multiple" class="selectpicker form-control" asp-items="ViewBag.Officer">
</select>
<span asp-validation-for="GetExistingSelectedOfficerIds" class="text-danger"></span>
asp-for="SecurityLog.Officer"
is useless since you are selecting list of officer ID – Ryan