Hi all again
I was almost sure I would come back here since I see working in ASP.NET (MVC or whatever) with a DB that has more than one tables related is complex, not the simple like working with only one table.
Below I share my NewOrUpdate.cshtml (the View to add a new Contact and his Telephone objects or Update the Contact and his Telephone objects; but it happens that the objects are not only one row in the DB but many. So I'm not sure the approach I'm implementing is the correct, if not (very possible) I ask you to give me some ideas about the correct)
First I share the Models (they are as you told me before and the FK is OK by EF Core):
Another thing I don't know is how to access the <input type="text">'s value (below the Telephones label) from de AddTelephone() C# method, to add new Telephone objects in Create or in Update..
Again, I really appreciate your help.
Thanks.
Pablo
I was almost sure I would come back here since I see working in ASP.NET (MVC or whatever) with a DB that has more than one tables related is complex, not the simple like working with only one table.
Below I share my NewOrUpdate.cshtml (the View to add a new Contact and his Telephone objects or Update the Contact and his Telephone objects; but it happens that the objects are not only one row in the DB but many. So I'm not sure the approach I'm implementing is the correct, if not (very possible) I ask you to give me some ideas about the correct)
First I share the Models (they are as you told me before and the FK is OK by EF Core):
C#:
public class Contact
{
public int ContactId { get; set; }
public string FullName { get; set; }
public string EMail { get; set; }
public string Address { get; set; }
public List<Telephone> Telephones { get; set; }
public bool Active { get; set; }
public Contact()
{
Telephones = new List<Telephone>();
}
}
public class Telephone
{
public int TelephoneId { get; set; }
public string Description { get; set; }
public string PhoneNumber { get; set; }
public int ContactId { get; set; }
public Contact Contact { get; set; }
}
C#:
@{
private void AddTelephone()
{
}
}
@model WebAppContactos.Models.Contact;
<div class="row">
<div class="col-sm-8 offset-sm-2">
<h2> New/Edit Contact </h2>
<form asp-controller="Contact" asp-action="NewEdit" method="post">
<input asp-for="ContactId" type="hidden" />
<div class="mb-3">
<label>Full Name</label>
<input class="form-control" asp-for="FullName" type="text" />
</div>
<div class="mb-3">
<label>EMail</label>
<input class="form-control" asp-for="EMail" type="text" />
</div>
<div class="mb-3">
<label>Address</label>
<input class="form-control" asp-for="Address" type="text" />
</div>
<div mb-3>
<label>Telephones</label>
<label>Phone Number</label> <label>Description</label>
<input type="text" class="form-control" /> <input type="text" class="form-control" /> <input type="button" class="btn btn-primary" onclick="AddTelephone"/>
<table class="table table-bordered">
<thead>
<tr>
<th>Description</th>
<th>Phone Number</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Telephones)
{
<tr>
<td> @item.Description </td>
<td> @item.PhoneNumber </td>
<td> <input type="button" class="btn btn-secondary">Update</input> </td>
<td> <input type="button" class="btn btn-danger">Delete</input> </td>
</tr>
}
</tbody>
</table>
</div>
<div class="mb-3">
<label>Active</label>
<input class="form-control" asp-for="Active" type="text" />
</div>
</form>
</div>
</div>
Another thing I don't know is how to access the <input type="text">'s value (below the Telephones label) from de AddTelephone() C# method, to add new Telephone objects in Create or in Update..
Again, I really appreciate your help.
Thanks.
Pablo