Pre-filled model returns null in Httppost

Asked

Viewed 100 times

2

I’m building a page in the ASP.NET project where I have a model that contains 2 lists, which I fill one initially at get and then direct to the View where I fill another list, but when receiving this object, it returns me with all the data null, nor the initial data I filled in 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");
    }

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.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.ValidationMessageFor(model => model.ChamadoAvaliacao[i].Nota, "", new { @class = "text-danger" })
                </div>
            </div>
        }

    }
  • 1

    I’ve had a similar problem, I made give the post via javascript even, by ajax...

  • @Marcogiovanni I am researching the life cycle of the model in the view, because I do not know if it invalidates soon after rendering the page or keeps until the post, if it invalidates soon in rendering any post will not help...

1 answer

0

In essence, primitive data types Timespan, Datetime, Guid, Decimal, String, etc are well "perceived" by Modelbinding. What you want is to POST the controller and pass a complex type of data and this also contains complex type variables.

public class ChamadoAvaliacaoViewModel

contains

public List<ChamadoTipoAvaliacao> ChamadoTiposAvaliacao { get; set; }
public List<ChamadoAvaliacao> ChamadoAvaliacao { get; set; }

You have two solutions:

The first is hardcode html that contains Id s and Names according to the List.

The second is to have in Viewmodel an array for each list so that Binding is performed:

public class ChamadoTiposAvaliacaoViewModel{

public List<ChamadoTiposAvaliacao> ChamadoTiposAvaliacao{ get; set; }
public ChamadoTiposAvaliacaoPostViewModel[] ChamadoTiposAvaliacaoArray { get; set; }}

View of the second option

@for (var itemCnt = 0; itemCnt < Model.ChamadoTiposAvaliacaoArray.Count(); itemCnt++){
<tr>
    <td></td>
    <td>
        @Html.TextBoxFor(m => Model.ChamadoTiposAvaliacaoArray[itemCnt].campo1)
        @Html.HiddenFor(m => Model.ChamadoTiposAvaliacaoArray[itemCnt].campo2)
</td></tr>}

[HttpPost]
public ActionResult AlgumaPostAction(int id, ChamadoTiposAvaliacaoViewModel model)
{
    //...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.