2
I have a class Collaborator, and a class Addressee. the collaborator has an address. In my collaborator class I have a:
public virtual Endereco Endereco { get; set; }
[ForeignKey("Endereco")]
public int? IdEndereco { get; set; }
This is my Create Contributor view:
<div class="row form-group">
<div class="col col-md-2">
<label for="text-input" class="form-control-label">Rua:</label>
</div>
<div class="col-lg-10" ;>
@Html.EditorFor(model => model.Endereco.EndRua, new { htmlAttributes = new { @class = "form-control-sm", style = "min-width:100%;", type = "text", id = "text-input", name = "text-input", placeholder = "Rua | Avenida" } })
@Html.ValidationMessageFor(model => model.Endereco.EndRua, "", new { @class = "text-danger" })
@*<input style="min-width:100%;" type="text" id="text-input" name="text-input" placeholder="Rua | Avenida" class="form-control-sm">*@
</div>
</div>
<div class="row form-group ">
<div class="col col-md-2">
<label for="email-input" class=" form-control-label">Número:</label>
</div>
<div class="col-md-1">
@Html.EditorFor(model => model.Endereco.EndNumero, new { htmlAttributes = new { @class = "form-control-sm", style = "width:59px;", type = "text", id = "text-input", name = "text-input", placeholder = "N°" } })
@Html.ValidationMessageFor(model => model.Endereco.EndNumero, "", new { @class = "text-danger" })
@*<input style="width:59px;" type="text" id="numero-input" name="nascimento-input" placeholder="Número" class="form-control-sm">*@
</div>
<div class="offset-md-2">
<div class="col col-md-2">
<label for="cpf-input" class=" form-control-label">Bairro:</label>
</div>
<div class="col-md-10">
@Html.EditorFor(model => model.Endereco.EndBairro, new { htmlAttributes = new { @class = "form-control-sm", style = "margin-left:-2px; width:237px;", type = "email", id = "text-input", name = "text-input", placeholder = "Bairro" } })
@Html.ValidationMessageFor(model => model.Endereco.EndBairro, "", new { @class = "text-danger" })
@*<input style="margin-left:-2px; width:237px;" type="text" id="bairro-input" name="bairro-input" placeholder="Bairro" class="form-control-sm">*@
</div>
</div>
</div>
<div class="row form-group ">
<div class="col col-md-2">
<label for="complemento-input" class=" form-control-label">Complemento:</label>
</div>
<div class="col-md-1">
@Html.EditorFor(model => model.Endereco.EndComplemento, new { htmlAttributes = new { @class = "form-control-sm", style = "width:270px;", type = "text", id = "text-input", name = "text-input", placeholder = "Complemento" } })
@Html.ValidationMessageFor(model => model.Endereco.EndComplemento, "", new { @class = "text-danger" })
@*<input style="width:270px;" type="text" id="complemento-input" name="complemento-input" placeholder="Complemento" class="form-control-sm">*@
</div>
<div class="offset-md-5" style="margin-left:261px;">
<div class="col col-md-2">
<label for="cpf-input" class=" form-control-label">CEP:</label>
</div>
<div class="col-md-10">
@Html.EditorFor(model => model.Endereco.EndCep, new { htmlAttributes = new { @class = "form-control-sm", style = "width:90px;", type = "text", id = "text-input", name = "text-input", placeholder = "Cep" } })
@Html.ValidationMessageFor(model => model.Endereco.EndCep, "", new { @class = "text-danger" })
@*<input style="width:90px;" type="text" id="cep-input" name="cep-input" placeholder="Cep" class="form-control-sm">*@
</div>
</div>
My controller is like this:
public ActionResult Create(CadastrarColaboradorViewModel colaborador)
{
if (!ModelState.IsValid)
{
Colaborador colab = new Colaborador();
//Salva endereço do Colaborador
using (var db = new Contexto())
{
Endereco endereco = new Endereco
{
EndBairro = colaborador.Endereco.EndBairro,
EndCep = colaborador.Endereco.EndCep,
EndCidade = colaborador.Endereco.EndCidade,
EndComplemento = colaborador.Endereco.EndComplemento,
EndNumero = colaborador.Endereco.EndNumero,
EndRua = colaborador.Endereco.EndRua,
EndUf = colaborador.Endereco.EndUf
};
db.Endereco.Add(endereco);
db.SaveChanges();
var idEndereco = endereco.IdEndereco;
colab.IdEndereco = idEndereco;
}
//Salva Endereço Fim *-*-*-*
colab.Nome = colaborador.Nome;
colab.Cpf = colaborador.Cpf;
colab.DataDeNascimento = colaborador.DataDeNascimento;
colab.Email = colaborador.Email;
colab.EstadoCivil = colaborador.EstadoCivil;
colab.Funcao = colaborador.Funcao;
colab.Sexo = colaborador.Sexo;
colab.StatusDoColaborador = colaborador.StatusDoColaborador;
colab.TipoDeColaborador = colaborador.TipoDeColaborador;
db.ColaboradorDb.Add(colab);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(colaborador);
}
The problem is as follows, as I am saving the address and assigning the saved id to the contributor, the address is saved correctly, however, in some cases of the error, when saving the contributor, then the address is saved without a contributor.
I would like to know if there is a method where I can validate if the contributor was saved successfully, and if not, I would like to delete the address I have already saved in the bank.
You can use a
try{}catch(Exception e){}
to capture and treat these errors...– Lodi
Good afternoon. There is a tool called Transactionscope, where if there is an error in the application, it returns the steps. Note more in Transactionscope - Microsoft In your case I think it helps.
– Augusto Henrique