This code was written at the time the Entity Framework was in version 5, so possibly the logic of manipulating the phone collection may have changed. Please notify me by comments if any error condition is found for me to fix.
Use the package BeginCollectionItem
, available on Nuget:
http://www.nuget.org/packages/BeginCollectionItem/
The following tutorial documents as well as implementing the master-detail:
http://www.joe-stevens.com/2011/07/24/asp-net-mvc-2-client-side-validation-for-dynamic-fields-added-with-ajax/
Would look like this:
_Createoredit.cshtml (Client)
@model SeuProjeto.Models.Cliente
@* Demais campos do seu model *@
@if (Model != null && Model.Telefones != null)
{
foreach (var telefone in Model.Telefones)
{
Html.RenderPartial("_TelefonesEditor", telefone);
}
}
@* Botões de submit, fechamento de <fieldset>, etc. *@
_Phoneseditor.cshtml
@model SeuProjeto.Models.Telefone
@using (Html.BeginCollectionItem("Telefones"))
{
@Html.HiddenFor(model => model.TelefoneID)
@Html.HiddenFor(model => model.ClienteID)
@Html.EditorFor(model => model.Telefone)
}
Customersscontroller.Cs
namespace SeuProjeto.Controllers
{
public class ClientesController : Controller
{
[HttpPost]
public ActionResult Create(Cliente cliente)
{
if (ModelState.IsValid)
{
if (shop.Telefones != null)
{
foreach (var telefone in cliente.Telefones)
{
telefone.ClienteID = cliente.ClienteID;
context.Entry(telefone).State = System.Data.Entity.EntityState.Modified;
context.SaveChanges();
}
}
}
// Lógica adicional, caso Model não seja válido
}
[HttpPost]
public ActionResult Edit(Cliente cliente)
{
if (ModelState.IsValid)
{
// Telefones Originais
List<Telefones> telefonesOriginais = context.Telefones.AsNoTracking().Where(t => t.ClienteID == cliente.ClienteID).ToList();
if (cliente.Telefones != null)
{
// Telefones Excluídos
foreach (var telefone in telefonesOriginais)
{
if (!cliente.Telefones.Where(t => t.TelefoneID == telefone.telefoneID).Any())
{
var telefoneExcluido = context.Telefones.Single(t => t.TelefoneID == telefone.TelefoneID);
context.Telefones.Remove(telefoneExcluido);
context.SaveChanges();
}
}
// Telefones Novos ou Editados
foreach (var telefone in cliente.Telefones)
{
if (telefone.ClienteID == 0)
{
telefone.ClienteID = cliente.ClienteID;
context.Telefones.Add(telefone);
}
else
{
context.Entry(telefone).State = System.Data.Entity.EntityState.Modified;
}
context.SaveChanges();
}
}
context.Entry(cliente).State = System.Data.Entity.EntityState.Modified;
context.SaveChanges();
}
// Lógica adicional, caso Model não seja válido
}
}
}
What is a master-detail record?
– Tiago César Oliveira
@Tiagocésaroliveira I believe it must be some entity of his system, and detail probably more details. Anyway, it is not clear. This question should be reformulated.
– Ricardo
Master-Detail, means a parent record with several children. Example, Client (master) and Telephone (detail). I only found examples with the saved master, not saving everything in a single post.
– Tiedt Tech
@Marlon.Tiedt, can you be clearer in your doubt? If you can isolate it in code, so much the better.
– Tiago César Oliveira
If you have the Customer model and the Phone model, I see no difficulty in applying the model... A simple
<input type="hidden" />
worthwhile0
if it is a new client or the ID value of theCliente
if it is an update would solve your problem.– Tiago César Oliveira