Hello All,
I have the following two model classes:
public partial class EmployeeInfo
{
public int EmployeeInfoId { get; set; }
public string LastName { get; set; } = null!;
[DisplayName("First Name")]
public string FirstName { get; set; } = null!;
public virtual ICollection<EmergencyInfo> EmergencyInfos { get; } = new List<EmergencyInfo>();
}
public partial class EmergencyInfo
{
public int EmployeeInfoId { get; set; }
public string LastName { get; set; } = null!;
public string FirstName { get; set; } = null!;
public string EmailAddress { get; set; } = null!;
public virtual EmployeeInfo EmployeeInfo { get; set; } = null!;
}
The relationship between EmployeeInfo table and EmergencyInfo table
is the foreign key EmployeeInfoId. The EmailAddress, FirstName, LastName
are the required, non null fields in the database. There could only be maximum two
emergency information so I have a loop that shows the input boxes for two emergency
information. WhenI fill out one of the emergency information data and pass the value
to the controller. The List for both emergency Information comes to this method:
public async Task<IActionResult> Create(EmployeeInfo employeeInfo)
{
if (ModelState.IsValid)
{
await _employeeService.AddEmployee(employeeInfo);
return RedirectToAction(nameof(Create));
}
return View(employeeInfo);
}
Below is the screen shot for both the list values. Now since, I am only filling out the first emergency information, I pass NULL for second emergency info EmailAddress, FirstName, LastName and that throws an error saying, email address, FirstName and LastName is required. How can I avoid this?
Below is the screen shot for the second list item that doesnt have any value and it is passing NULL to the database and I get an exception that NULL value is not allowed in the Database:
My second question is, I am prepopulating the fields of employeeInfo
on my cshtml file. In order to achieve this, I am doing this:
public async Task<IActionResult> Create()
{
employee = await _employeeService.GetEmployeeByEmployeeNumber(EmployeeId);
return View(employee);
}
public async Task<EmployeeInfo?> GetEmployeeByEmployeeNumber(string employeeId)
{
var employee = await _ackContext.EmployeeInfos.OrderBy(e => e.EmployeeNumber).LastOrDefaultAsync(e => e.EmployeeNumber == employeeId);
return employee;
}
This only populates my employeeInfo input boxes and ignores the emergencyInfo input boxes. How can I modify my linq above so that employee can have both EmployeeInfo data and emergencyInfo data.
This is how my cshtml file looks like:
<div class="form-group row">
<div class="col-sm-4">
<label asp-for="LastName" class="control-label"></label>
<input asp-for="LastName" class="form-control input-lg" />
<span asp-validation-for="LastName" class="text-danger"></span>
</div>
<div class="col-sm-4">
<label asp-for="FirstName" class="control-label"></label>
<input asp-for="FirstName" class="form-control input-lg" />
<span asp-validation-for="FirstName" class="text-danger"></span>
</div>
</div>
@for (var i = 0; i < 2; i++)
{
<div class="form-group row">
<div class="col">
<label asp-for="@Model.EmergencyInfos.ToList()[i].LastName" class="control-label"></label>
<input name="EmergencyInfos[@i].LastName" class="form-control" />
<span asp-validation-for="@Model.EmergencyInfos.ToList()[i].LastName" class="text-danger"></span>
</div>
<div class="col">
<label asp-for="@Model.EmergencyInfos.ToList()[i].FirstName" class="control-label"></label>
<input name="EmergencyInfos[@i].FirstName" class="form-control" />
<span asp-validation-for="@Model.EmergencyInfos.ToList()[i].FirstName" class="text-danger"></span>
</div>
<div class="col">
<label asp-for="@Model.EmergencyInfos.ToList()[i].EmailAddress" class="control-label"></label>
<input name="EmergencyInfos[@i].EmailAddress" class="form-control" />
<span asp-validation-for="@Model.EmergencyInfos.ToList()[i].EmailAddress" class="text-danger"></span>
</div>
</div>
}
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
any help will be greatly appreciated.