0

I'm building a page in ASP.NET project where I have a model that contains two lists, which fill an initially get in and then direct it to the view where you fill another list, but to get this object, it returns me with all the data in null or the initial data I filled it comes.

ViewModel

public class ChamadoAvaliacaoViewModel
{
    public int Id { get; set; }
    public string Assunto { get; set; }
    [Display(Name = "Descrição")]
    public string Descricao { get; set; }
    [Display(Name = "Responsavel do Chamado")]
    public ApplicationUser ResponsavelChamado { get; set; }
    [Display(Name = "Solução")]
    public string Solucao { get; set; }
    [Display(Name = "Avaliação do Chamado")]
    public int AvaliacaoChamado { get; set; }
    [Display(Name = "Justificativa da Avaliação")]
    public string JustificativaAvaliacao { get; set; }
    public List<ChamadoTipoAvaliacao> ChamadoTiposAvaliacao { get; set; }
    public List<ChamadoAvaliacao> ChamadoAvaliacao { get; set; }
}

Actions

    [Authorize]
    public ActionResult AvaliacaoChamado(string id)
    {
        var chamado = new ChamadoDAO(db).BuscarChamadoId(Convert.ToInt32(id));
        if (chamado.StatusChamado.Value)
        {
            var model = new ChamadoAvaliacaoViewModel();
            model.Id = chamado.Id;
            model.Descricao = chamado.Descricao;
            model.Assunto = chamado.Assunto;
            model.Solucao = chamado.Solucao;
            model.ResponsavelChamado = chamado.ResponsavelChamado;
            model.ChamadoTiposAvaliacao = new ChamadoTipoAvaliacaoGN(db).retornarChamadoTipoAvaliacao();
            model.ChamadoAvaliacao = new List<ChamadoAvaliacao>();
            foreach (var ChamadoTipoAvaliacao in model.ChamadoTiposAvaliacao)
            {
                model.ChamadoAvaliacao.Add(new ChamadoAvaliacao());
            }
            return View(model);
        }
        else
        {
            TempData["notice"] = "Chamado não pode ser avaliado, pois ele não foi encerrado.";
            return RedirectToAction("Index", "Chamado");
        }
    }

    [HttpPost]
    public ActionResult AvaliacaoChamado(string id, ChamadoAvaliacaoViewModel chamadoAvaliacao)
    {
        var aGN = new ChamadoAvaliacaoGN(db);
        var cGN = new ChamadoGN(db);
        var chamado = new ChamadoDAO(db).BuscarChamadoId(Convert.ToInt32(id));
        foreach (var avaliacao in chamadoAvaliacao.ChamadoAvaliacao)
        {
            aGN.registrarAvaliacao(avaliacao);
        }
        cGN.RegistrarUltimaInteracao(Convert.ToInt32(id));
        TempData["notice"] = "Chamado Avaliado com Sucesso!";
        return RedirectToAction("Index", "Home");
    }

Part of View

@for (int i = 0; i < Model.ChamadoTiposAvaliacao.Count; i++)
    {
        if (Model.ChamadoTiposAvaliacao[i].Id == 1)
        {
            <div class="form-group">
                @Html.Label(Model.ChamadoTiposAvaliacao[i].Titulo, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextAreaFor(model => model.ChamadoAvaliacao[i].Justificativa, htmlAttributes: new { @class = "form-control", Style = "height:130px" })
                    @Html.HiddenFor(it => Model.ChamadoAvaliacao[i].Justificativa)
                    @Html.ValidationMessageFor(model => model.ChamadoAvaliacao[i].Justificativa, "", new { @class = "text-danger" })
                </div>
            </div>
        }
        else
        {
            <div class="form-group">
                @Html.Label(Model.ChamadoTiposAvaliacao[i].Titulo, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextAreaFor(model => model.ChamadoAvaliacao[i].Nota, htmlAttributes: new { @class = "rating rating-loading AvaliacaoChamado" })
                    @Html.HiddenFor(it => Model.ChamadoAvaliacao[i].Nota)
                    @Html.ValidationMessageFor(model => model.ChamadoAvaliacao[i].Nota, "", new { @class = "text-danger" })
                </div>
            </div>
        }

    }
  • Change the parameter name to `ChamadoAvaliacaoViewModel model` (or anything that does not match the name of a property in your model). And why are you generating a `HiddenFor()` in the view (it will be ignored since you already have rendered a `TextAreaFor()`)?. And why are you generating labels that have no relationship to the associated form control? –  Sep 13 '16 at 12:10
  • I will test, about HiddenFor was removed, i forgot of remove of code. thks! – Anderson Souza Sep 13 '16 at 12:39
  • I changed the model to modelAvaliacao, but don't works, the model in httppost return instancied but the fields return null – Anderson Souza Sep 13 '16 at 12:59
  • No idea what you mean by _return instancied_ - there is no property named `instancied`. The dupe explains why the parameter `ChamadoAvaliacaoViewModel chamadoAvaliacao` was `null`. Not sure if you mean the the model is still `null` or just its properties are `null`after changing the parameter name, but in any case, the code you have shown will work fine, so its code that you have not shown that is the issue. –  Sep 13 '16 at 13:06
  • Ok, i will better explain, the parameter 'ChamadoAvaliacaoViewModel chamadoAvaliacao' wasn't null, but the properties are null. – Anderson Souza Sep 13 '16 at 13:31

0 Answers0