Controller receiving null parameters

Asked

Viewed 185 times

0

inserir a descrição da imagem aqui//Controller

[HttpPost]
public ActionResult EditarConfSist(ConfSistMOD confSist)
{
    try
    {
        var repositorio = new ConfSistREP();
        repositorio.EditarConfSist(confSist);


        // gerar o log 
        logar = new LogREP();

        log = new LogMOD();
        log.NomeUsuario = HttpContext.User.Identity.Name;
        log.Login = VariaveisDeSessao.LoginUsuario;
        log.Dt_Log = DateTime.Now;
        log.IP = Request.UserHostAddress;
        log.HostName = VariaveisDeSessao.GetHostName(Request.ServerVariables);

        log.Acao = "Apelido " + confSist.dsApelido + " (valor = " + confSist.dsValor + ") foi editado com sucesso.\n"
            + "Detalhes: \n"
            + "Valor = Antes: " + repositorio.dsValorAntigo.dsValor + " \t\tDepois: " + confSist.dsValor + "\n"
            + "dsApelido = Antes: " + repositorio.dsValorAntigo.dsApelido + " \t\tDepois: " + confSist.dsApelido + "\n"
            + "dsDescrição = Antes: " + repositorio.dsValorAntigo.dsDescricao + " \t\tDepois: " + confSist.dsDescricao + "\n";

        //log.IdLogIdTabela = confSist.dsApelido.ToString();
        //string wRetorno = logar.InserirLog(log);

        TempData["Sucesso"] = "Valor editado com Sucesso";

        return RedirectToAction("ConfiguracoesDeSistema");
    }
    catch (Exception ex)
    {// gerar o log
        logar = new LogREP();

        log = new LogMOD();
        log.NomeUsuario = HttpContext.User.Identity.Name;
        log.Login = VariaveisDeSessao.LoginUsuario;
        log.Dt_Log = DateTime.Now;
        log.IP = Request.UserHostAddress;
        log.HostName = VariaveisDeSessao.GetHostName(Request.ServerVariables);

        //log.Acao = "Falha ao Editar valor " + confSist.dsApelido + " (valor = " + confSist.dsValor + "). \nMensagem: " + ex.Message + "" + ex.StackTrace;
        //logar.InserirLog(log);

        TempData["Erro"] = "Falha ao editar valor";

        return RedirectToAction("ConfiguracoesDeSistema");
    }
}

// Model

public class ConfSistMOD
{
    //RSS - 15/02/2019

    public string ID { get; set; }

    [Display(Name = "Apelido")]
    public string dsApelido { get; set; }

    [Display(Name = "Descrição")]
    public string dsDescricao { get; set; }

    [Display(Name = "Valor")]
    public string dsValor { get; set; }

    [Display(Name = "Tipo")]
    public Int16 dsTipo { get; set; }

}

//View

public class ConfSistViewModel
{
   /// model principal
    public ConfSistMOD Config { get; set; }

    public List<ConfSistMOD> ListaConfiguracoes { get; set; }

    //public SelectList Filtros { get; set; }

    public ConfSistViewModel()
    {
        //Inicializar lista
        Config = new ConfSistMOD();
        ConfSistREP confSistCaduREP = new ConfSistREP();
        this.ListaConfiguracoes = confSistCaduREP.CarregarListaDeConfiguracoes();

    }
}

//Repositotio

namespace DataAccess
{
    public class ConfSistREP
    {
        public ConfSistMOD dsValorAntigo { get; set; }
        public string EditarConfSist(ConfSistMOD confSist)
        {
            using (var conexao = new ConexaoBanco())
            {
                var tbConf = new tbConfiguracoes();
                tbConf.dsApelido = confSist.dsApelido;
                tbConf.dsDescricao = confSist.dsDescricao;
                //tbConf.dsTipo = confSist.dsTipo;
                tbConf.dsValor = confSist.dsValor;

                conexao.tbConfiguracoes.Add(tbConf);
                conexao.SaveChanges();

                return tbConf.dsApelido;
            }
        }

        <div class="form-group">
            <table class="table table-striped table-hover">

                <thead>
                    <tr>
                        <th>Apelido</th>
                        <th>Descrição</th>
                        <th>Valor</th>
                        <th>Editar</th>
                    </tr>
                </thead>

                <tbody>

                    @foreach (var item in Model.ListaConfiguracoes)
                    {
                        using (Html.BeginForm("EditarConfSist", "Lojas", FormMethod.Post, new { @class = "form-horizontal" }))
                        {
                            @Html.AntiForgeryToken()

                            <tr data-id="@item.ID">

                                <td>
                                    @Html.TextBox("model.dsApelido", @item.dsApelido, new { @class = "form-control", @readonly = "readonly" })
                                </td>
                                <td>
                                    @Html.TextBox("model.dsDescricao", @item.dsDescricao, new { @class = "form-control", @readonly = "readonly" })
                                </td>
                                <td>
                                    @if (@item.dsTipo == 1)
                                    {
                                        if (@item.dsValor == "Sim")
                                        {
                                            <select name="model.Dropdownlist">
                                                <option value="False">Não</option>
                                                <option value="True" selected>Sim</option>
                                            </select>
                                        }
                                        else
                                        {
                                            <select name="model.Dropdownlist">
                                                <option value="False" selected>Não</option>
                                                <option value="True" >Sim</option>
                                            </select>

                                        }
                                    }
                                    else
                                    {
                                        @Html.TextBox("model.dsValor", item.dsValor, new { @class = "form-control" })
                                    }
                                </td>


                                @Html.Hidden("model.ID", item.ID)
                               @* @Html.HiddenFor(model => model.Opc)*@

                                <td align="center">
                                    <button id="EditarSistConf" type="submit" style="border:none; background-color:transparent"><img src="~/Content/Icones/editar1.png" /></button>
                                </td>
                            </tr>
                        }
                    }
                </tbody>
            </table>
        </div>
    }
}
  • 1

    Rename your textbox, remove these model..

  • @Pedropaulo solved ! Now he’s giving another Exception .. Do you have any idea what it might be? : ( {"Unable to update the Entityset 'tbConfigurations' because it has a Definingquery and no <Insertfunction> element exists in the <Modificationfunctionmapping> element to support the Current Operation."}

  • 1

    Great! I’ll post the answer to help other people who have the same problem. As for the new problem, by forum rules, you should open a new question to this question, as it is a different problem from the original question. Hug!

  • Okay @Pedropaulo thanks.

1 answer

1


You are performing a POST in your page form. So that Submit sends the data to the server and its Controller recognize the parameters of your class ConfSistMOD, we need to name the fields within the form equal to the names of your class ConfSistMOD.

To resolve the issue, remove the names model. of all your fields and leave equal the properties of your class.

Example:

@Html.TextBox("dsDescricao", @item.dsDescricao, new { @class = "form-control", @readonly = "readonly" })

Browser other questions tagged

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