Hi @Anjali Agarwal
public virtual ICollection<EmergencyInfo> EmergencyInfos { get; set; }
Since you have already configured one-to-many relationship between the employeeInfo and emergencyInfo model, in the EmployeeInfo Create or Edit page, you can display the emergencyInfo via the above Navigation property (EmergencyInfos).
For example:
In the Edit view page: Use for
statement to loop through the EmergencyInfos, in this sample, I just test the EmailAddress property, you can add other properties. And note the name attribute, we will set its name based on the Page Model.
@model WebApplication5.Models.EmployeeInfo
@{
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="EmployeeInfoId" class="control-label"></label>
<input asp-for="EmployeeInfoId" class="form-control" />
<span asp-validation-for="EmployeeInfoId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="PolicyYear" class="control-label"></label>
<input asp-for="PolicyYear" class="form-control" />
<span asp-validation-for="PolicyYear" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DateFormFilled" class="control-label"></label>
<input asp-for="DateFormFilled" class="form-control" />
<span asp-validation-for="DateFormFilled" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="EmployeeNumber" class="control-label"></label>
<input asp-for="EmployeeNumber" class="form-control" />
<span asp-validation-for="EmployeeNumber" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="LastName" class="control-label"></label>
<input asp-for="LastName" class="form-control" />
<span asp-validation-for="LastName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="FirstName" class="control-label"></label>
<input asp-for="FirstName" class="form-control" />
<span asp-validation-for="FirstName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="MiddleName" class="control-label"></label>
<input asp-for="MiddleName" class="form-control" />
<span asp-validation-for="MiddleName" class="text-danger"></span>
</div>
@for (var i = 0; i< Model.EmergencyInfos.Count; i++)
{
<hr />
<span>Emergency @i</span>
<div class="form-group">
<label asp-for="@Model.EmergencyInfos.ToList()[i].EmailAddress" class="control-label"></label>
<input asp-for="@Model.EmergencyInfos.ToList()[i].EmailAddress" name="EmergencyInfos[@i].EmailAddress" class="form-control" />
<span asp-validation-for="@Model.EmergencyInfos.ToList()[i].EmailAddress" class="text-danger"></span>
</div>
}
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
Then, in the controller: [Note] The Bind attribute can be applied to a class or a method parameter. Specifies which properties of a model should be included in model binding. Since you want to send the complex model and all properties to the post method, there is no need to add the Bind attribute. More detail information, see Model Binding in ASP.NET Core.
public IActionResult Edit()
{
var item = new EmployeeInfo()
{
EmployeeInfoId= 1,
FirstName ="Fname",
LastName ="Lname",
MiddleName="Mname",
PolicyYear=10,
EmployeeNumber= "n1001",
DateFormFilled =DateTime.Now,
EmergencyInfos = new List<EmergencyInfo>()
{
new EmergencyInfo()
{
EmailAddress="******@hotmail.com"
},
new EmergencyInfo()
{
EmailAddress="******@hotmail.com"
}
}
};
return View(item);
}
[HttpPost]
public IActionResult Edit(int id, EmployeeInfo employeeInfo)
{
if (ModelState.IsValid)
{
return RedirectToAction(nameof(Index));
}
return View();
}
The result as below:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion