0

Here's my cshtml code:

@model pedidosOnlineMVC.Models.Administrador
@{
    Layout = "~/Views/SuperUser/_LayoutSU.cshtml";
}
@using (var f = Html.Bootstrap().Begin(new Form()))
{
    @f.FormGroup().CustomControls(Html.AntiForgeryToken())
    @f.FormGroup().TextBoxFor(model=>model.nome)
    @f.FormGroup().TextBoxFor(model=>model.cpf).Data(new {mask="999.999.999-99"})
    @f.FormGroup().TextBoxFor(model=>model.telefone).Data(new {mask="(99)99999-9999"})
    @f.FormGroup().TextBoxFor(model=>model.login)
    @f.FormGroup().PasswordFor(model=>model.senha)
    @f.FormGroup().TextBoxFor(model => model.fim_gerencia).Data(new { provide = "datepicker", date_format = "mm/dd/yyyy" })
    @f.FormGroup().CustomControls(@Html.Bootstrap().SubmitButton().Text("Cadastrar"))
}

And here's view's controller:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult cadAdmin([Bind(Include = "Administrador_Id, nome, cpf, telefone, login, senha, fim_gerencia")] Administrador adm)
    {
        if (ModelState.IsValid)
        {
            if (Helpers.Helpers.validaCPF(adm.cpf))
                if (db.Administrador.ToList().Where(x => x.cpf == adm.cpf).ToArray().Length == 0)
                {
                    adm.senha = System.Text.Encoding.Default.GetString(SHA1.Create().ComputeHash(System.Text.Encoding.UTF8.GetBytes(adm.senha)));
                    db.Administrador.Add(adm);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("cpf", "CPF já cadastrado");
                    return View("Index", "Index");
                }
            else
            {
                ModelState.AddModelError("cpf", "CPF inválido");
                return View(adm);
            }
        }
        Type type = adm.GetType();
        PropertyInfo[] props = type.GetProperties();
        for (int i = 2; i < props.Length; i++)
            if (ModelState.Values.ToList()[i-2].Errors.Count>0)
                ModelState.AddModelError(props[i].Name, props[i].Name + " inválido");
        return PartialView(adm);
    }

The problem that I have is that the date value from the field fim_gerencia is not passing through. Everything else works fine, excepet the Date. I'm using the datepicker found here: http://bootstrap-datepicker.readthedocs.org/en/release/ It shouldn't be a problem though, beacause I'm using date on another class and it works fine with he exact same code. Anyone know what's wrong?

1 Answers1

1

In your situation, first thing I would check is the id of the textbox of the date field. Does it contain the expected id value? e.g.

<input ..... id="fim_gerencia"......

If that's correct, then open the Network window in the browser dev tools. When you submit the page, have a look at the request by clicking on it in the Network page. Take a look at the request details, a section of which will be the form values passed to the server. In that section look for fim_gerencia.

Do those first.

If it's still not being passed, try removing the [Bind(Include=.... bit, as you don't need that in your example. It's worth removing that attribute and checking the result.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
  • Yes, the `id` was correct. Just tried doing it with the network window open and it worked! I did nothing else, just opened it and it worked. I have no idea what happened, but I'm marking this as the answer. Thanks! – Guilherme Lofrano Corneto Sep 20 '14 at 19:33