cshtml页面未在发布时传递日期值 [英] cshtml page not passing date value on post

查看:61
本文介绍了cshtml页面未在发布时传递日期值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的cshtml代码:

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"))
}

这是视图的控制器:

[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);
    }

我遇到的问题是, fim_gerencia 字段中的日期值未通过.一切正常,请超越日期.我正在使用此处找到的日期选择器: http://bootstrap-datepicker.readthedocs.org/en/release/但是,这应该不成问题,因为我正在另一个类上使用日期,并且与他完全相同的代码也可以正常工作.有人知道怎么了吗?

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?

推荐答案

在您的情况下,我要检查的第一件事是日期字段文本框的 id .它是否包含预期的id值?例如

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" ......

如果正确,请在浏览器开发工具中打开 Network 窗口.提交页面时,在 Network 页面上单击该请求即可查看该请求.查看请求详细信息,其中一部分将是传递给服务器的表单值.在该部分中查找 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.

先做那些.

如果仍然没有通过,请尝试删除 [Bind(Include = .... )位,因为在示例中您不需要这样做.值得删除该属性并检查结果.

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.

这篇关于cshtml页面未在发布时传递日期值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆