3
I have the following models:
Models
public class Atividade : BaseEntity
{
public virtual int Id{ get; set; }
public virtual string Descricao{ get; set; }
public virtual IList<Anotacao> Anotacoes{ get; set; }
}
public class Anotacao : BaseEntity
{
public virtual int Id{ get; set; }
public virtual Atividade Atividade{ get; set; }
public virtual string Motivo{ get; set; }
}
The problem is when I’m changing my activity my Notes list is lost every time I save. how can I solve this problem?
View
@model SPACE.Model.Atividade
@using (Html.BeginForm("SalvarAtividade", "Atividade", FormMethod.Post, new { id = "formAtividade" }))
{
@Html.AntiForgeryToken()
<div class="form-body">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.Descricao, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Descricao, new { htmlAttributes = new { @class = "form-control", @required = "required" } })
<small>@Html.ValidationMessageFor(model => model.Descricao, "", new { @class = "text-danger" })</small>
</div>
<hr />
<div class="form-actions" align="right">
<a href="@Url.Action("GridAtividade", "Atividade", new { Area = "Cadastro" })" class="btn btn-danger waves-effect waves-light">
<i class="fa fa-times" aria-hidden="true"></i>
</a>
<button type="button" id="btn-salvar-atividade" title="Sem permissão de usuário." class="btn btn-success" onclick="validaDataHora();"> <i class="fa fa-check"></i> Salvar</button>
</div>
}
Scripts
$("#btn-salvar-atividade").click(function () {
debugger;
var formAtividade = $("#formAtividade").serialize();
$.ajax({
type: "POST",
//contentType: "application/json",
url: "@Url.Action("SalvarAtividade", "Atividade", new { area = "Cadastro" })",
data: JSON.stringify({
model: formAtividade
}),
success: function (data) {
return msg("Salvo com sucesso");
}
});
});
</script>
Controller
public async Task<ActionResult> SalvarAtividade(Atividade model)
{
using (var save = _session.BeginTransaction())
{
try
{
_session.SaveOrUpdate(model);
save.Commit();
return new RetornoSalvarDto { Model = model, ERetornoCrud = ERetornoCrud.Sucesso };
}
catch (Exception e)
{
save.Rollback();
_session.Clear();
string error = "Ops! Ocorreu um problema ao tentar salvar." + e.Message;
return new RetornoSalvarDto { Model = model, ERetornoCrud = ERetornoCrud.Erro };
}
}
}
Could you describe further details of your saving method? Probably Voce is saving an object that is not tied to his session and with that he removes all the joins with the activities that were before.
– Nícolas Tarzia
Updated with my save method... However, when the parameter arrives, it is enough to save with the zeroed list...
– Eluander J. F. Lopes
Post details of Retornosalvardto also
– George Wurthmann