Hello all,
I'm working on an MVC web application that consists of a Contact list with variable number of phone numbers. In what I'm stuck is in designing a dynamic table (as a part of the NewOrEdit view) that gives me the possibility of adding, editing, and deleting phone number objects. Supposedly I would need buttons and C# code for doing that. I tried to add C# code into @{ } to make something like a binding, because I have to know (in the C# code) the values I insert in the <input/>s I put into the <td> ... </td>. I tried but I could not, also many compiler errors appeared. By now I'm testing just Insert (or New), not Edit because it would be more difficult for you to help, but once Insert is fine, Edit will be very easy.
I give you the code I have by now, maybe you can help me to learn. I googled a lot but didn't find anything useful. I tried with javascript and ajax before but it didn't work.
1) The Contact Model
2) The Telephone Model
3) The NewOrEdit.cshtml View (where the big problem is - gives compiler errors)
4) Part of the ContactController (I think you need just this to test, debugging, if the data has been sent to the Post Action Method, looking in the contact parameter, you don't need the DB either)
I would appreciate your help very much. Please tell me if my explanation is not very clear.
Thank you
Pablo
Bs As Arg.
I'm working on an MVC web application that consists of a Contact list with variable number of phone numbers. In what I'm stuck is in designing a dynamic table (as a part of the NewOrEdit view) that gives me the possibility of adding, editing, and deleting phone number objects. Supposedly I would need buttons and C# code for doing that. I tried to add C# code into @{ } to make something like a binding, because I have to know (in the C# code) the values I insert in the <input/>s I put into the <td> ... </td>. I tried but I could not, also many compiler errors appeared. By now I'm testing just Insert (or New), not Edit because it would be more difficult for you to help, but once Insert is fine, Edit will be very easy.
I give you the code I have by now, maybe you can help me to learn. I googled a lot but didn't find anything useful. I tried with javascript and ajax before but it didn't work.
1) The Contact Model
Contact Model:
namespace WebAppContacts.Models
{
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; } = new List<Telephone>();
public bool IsActive { get; set; }
}
}
2) The Telephone Model
Telephone Model:
namespace WebAppContacts.Models
{
public class Telephone
{
public int TelephoneId { get; set; }
public string? Description { get; set; }
public string? PhoneNumber { get; set; }
public int ContactId { get; set; }
}
}
3) The NewOrEdit.cshtml View (where the big problem is - gives compiler errors)
NewOrEdit.cshtml View:
@model Contact;
@{
public string phoneNumber = "", description = "";
public void AddTelephone()
{
Telephone telephone = new Telephone();
telephone.PhoneNumber = phoneNumber;
telephone.Description = description;
Model.Telephones.Add(telephone);
}
}
<div class="row">
<div class="col-sm-8 offset-sm-2">
<h2> New/Edit Contact </h2>
<form asp-controller="Contact" asp-action="NewOrEdit" 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>E-Mail</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 class="mb-3">
<table class="table table-bordered">
<thead>
<tr>
<th>Description></th>
<th>Phone Number</th>
<th>Update</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.Telephones.Count; i++)
{
<tr>
<td> <input type="text" asp-for="@Model.Telephones[i].PhoneNumber" /> </td>
<td> <input type="text" asp-for="@Model.Telephones[i].Description" /> </td>
<td> <input type="button" class="btn btn-secondary" value="Update" /> </td>
<td> <input type="button" class="btn btn-danger" value="Remove" onclick="Remove(this)" /> </td>
</tr>
}
</tbody>
<tfoot>
<tr>
<td><input type="text" value=@phoneNumber /></td>
<td><input type="text" value=@description /></td>
<td><input type="button" class="btn btn-outline-success" value="Agregar" onclick="AddTelephone"/></td>
</tr>
</tfoot>
</table>
</div>
<div class="mb-3">
<label>Is Active</label>
<input class="form-control" asp-for="IsActive" type="text" />
</div>
<input type="submit" class="btn btn-outline-primary" value="Save contact" />
</form>
</div>
</div>
4) Part of the ContactController (I think you need just this to test, debugging, if the data has been sent to the Post Action Method, looking in the contact parameter, you don't need the DB either)
C#:
namespace WebAppContacts.Controllers
{
public class ContactController : Controller
{
public ContactController()
{
}
[HttpGet]
public IActionResult NewOrEdit()
{
Contact contact = new Contact();
return View(contact);
}
[HttpPost]
public IActionResult NewOrEdit(Contact contact)
{
// contents was deleted for practicality
// you can see, debugging, if data (especially the
// Telephone List<>) has reached the parameter
}
}
}
I would appreciate your help very much. Please tell me if my explanation is not very clear.
Thank you
Pablo
Bs As Arg.