验证错误未显示

时间:2016-05-21 08:07:04

标签: asp.net asp.net-mvc-4

大家好我正在开发一个我必须创建用户配置文件的项目,在我的get方法的用户配置文件中我想加载与用户相关的每个数据,然后当他点击提交将其发布到数据库时是验证错误没有显示,我不知道为什么,或如何解决它。

这是我的代码

获取控制器

public ActionResult EditarUtilizador()
    {
        var id = User.Identity.GetUserId();

        // Fetch the userprofile
        ApplicationUser user = db.Users.Include(u => u.Tags).FirstOrDefault(u => u.Id.Equals(id));
        preencherTagUtilizador(user);
        var role = (from s in db.Roles where s.Id == user.TipoUtilizador select s.Name).First();

        // Construct the viewmodel
        EditarUtilizadorViewModel model = new EditarUtilizadorViewModel();
        model.Utilizador = user.Nome;
        model.Email = user.Email;
        model.TipoUtilizador = role;
        model.DataNascimento = user.DataNascimento;
        model.Empresa = user.empresa;
        model.Sobre = user.Sobre;
        model.Iban = user.Iban;
        model.Telemóvel = user.Telemóvel;
        model.Website = user.website;
        model.ImagePath = user.ImagePath ;

        return View(model);
    }

后置控制器

[HttpPost]
    public async Task<ActionResult> EditarUtilizador(EditarUtilizadorViewModel model, string[] selectedTag, HttpPostedFileBase file)
    {
        var id = User.Identity.GetUserId();
        // Get the userprofile
        ApplicationUser user = db.Users.FirstOrDefault(u => u.Id.Equals(id));

        if (ModelState.IsValid)
        {

            if (file != null)
            {
                var fileName = id + "_" + file.FileName;
                file.SaveAs(HttpContext.Server.MapPath("~/Images/")
                                                      + fileName);
                model.ImagePath = fileName;
            }
            else
            {
                var filename = user.ImagePath;
                model.ImagePath = filename;
            }
            user.ImagePath = model.ImagePath;


            atualizarUtilizadorTag(user, selectedTag);
            // Update fields
            user.DataNascimento = model.DataNascimento;
            user.empresa = model.Empresa;
            user.Sobre = model.Sobre;
            user.Iban = model.Iban;
            user.Telemóvel = model.Telemóvel;
            user.website = model.Website;

            var result =  UserManager.CheckPassword(user, model.Password);

            if (result)
            {
                await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.Password, model.NovaPassword);
            }
            else
            {
                TempData["CustomError"] = "A sua password antiga não está correta";
            }

            db.Entry(user).State = EntityState.Modified;

            db.SaveChanges();
            preencherTagUtilizador(user);
            return RedirectToAction("Index", "Home"); // or whatever
        }
        preencherTagUtilizador(user);
        var caminho = user.ImagePath;
        model.ImagePath = caminho;
        model.Utilizador = user.Nome;
        model.Email = user.Email;
        return View(model);
    }

我的观点

@using (Html.BeginForm("EditarUtilizador", "Account", null, FormMethod.Post,
                                                  new { enctype = "multipart/form-data" }))
{
    System.Diagnostics.Debug.WriteLine(@Html.DisplayFor(modelItem => modelItem.ImagePath));
    <div class="form-horizontal">
        <h4>Editar Perfil Pessoal</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <fieldset class="col-md-3">
        @if (Model.ImagePath != null)
        {
            <img src="~/Images/@Html.DisplayFor(modelItem => modelItem.ImagePath)" width="150" height="150" />
            <div style="margin-top:10px">
                <input id="ImagePath" type="file" name="file" class="filestyle" data-input="false" data-buttonText="Altera a tua foto" />
            </div>
            <div style="margin-top:30px">
                <input id="ImagePath" type="file" name="file" class="filestyle" data-input="false" data-buttonText="Apagar Foto" />
            </div>
        }
        else
        {
            <img src="~/StaticImages/default-user-image.png" width="150" height="150" />
            <div style="margin-top:10px">
                <input id="ImagePath" type="file" name="file" class="filestyle" data-input="false" data-buttonText="Carrega  uma foto" />
            </div>
        }
    </fieldset>
    <div class="col-md-7">
        <div class="row">
            <div class="form-group">
                @Html.LabelFor(m => m.Utilizador, new { @class = "col-md-3 control-label" })
                <div class="col-md-9">
                    <p class="form-control-static">@Model.Utilizador</p>
                    @Html.ValidationMessageFor(model => model.Utilizador, "", new { @class = "text-danger" })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(m => m.Password, new { @class = "col-md-3 control-label" })
                <div class="col-md-9">
                    @Html.EditorFor(m => m.Password, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
                </div>
            </div>

1 个答案:

答案 0 :(得分:0)

请确保您的模型类具有验证的所有属性,并显示消息。

尝试清理并重建。分享模型以进一步检查并参考以下链接---

old question 1old question 2