一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性源错误db.SaveChanges();

问题描述 投票:2回答:1

首先,我使用实体框架代码首先从数据库MVC5 / C#。我想添加新用户,但当我尝试添加电子邮件时,我收到了错误消息。

这是我的控制器方法:

      [HttpPost]
      public ActionResult InsertUser(Users users, string radioActive) 
    { 
            Users newuser = new Users();
            newuser.username = users.username;
            newuser.password = users.password;
            newuser.name = users.name;
            newuser.mail = users.mail;
            if (radioActive == "on") newuser.isActive = true;
            else newuser.isActive = false;

            db.Users.Add(newuser);
            db.SaveChanges();

      return Redirect(Url.Content("~/Administrate/"));
    }

问题是db.SaveChanges();我不明白为什么,但除了email.Email返回null,我的所有值都返回良好,我得到他的错误。

这也是我的看法:

      <div class="panel panel-primary">
            <div class="panel-heading text-center">KULLANICI EKLE</div>
            <div class="panel-body">
                <form data-toggle="validator" role="form" action="~/Administrate/InsertUser" method="post">
                    <div class="form-group">
                        <label for="username" class="control-label">Kullanıcı Adı</label>
                        <input type="text"  class="form-control" id="username" name="username"placeholder="Lütfen kullanıcı adını giriniz." required>
                    </div>
                    <div class="form-group">
                        <label for="password" class="control-label">Şifre</label>
                        <div class="form-inline row">
                            <div class="form-group col-sm-6">
                                <input type="password" name="password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}"  class="form-control" id="password" placeholder="Şifre" required>
                                <div class="help-block">Şifreniz ez bir sayı,bir büyük ve bir küçük harf içermeli ve minimum 6 karakterden oluşmalıdır.</div>
                            </div>
                            <div class="form-group col-sm-6">
                                <input type="password" class="form-control" name="passwordConfirm" id="passwordConfirm" data-match="#password" data-match-error="Şifreniz eşleşmiyor" placeholder="Şifre Tekrar" required>
                                <div class="help-block with-errors"></div>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="name" class="control-label">Çalışan Adı Soyadı</label>
                        <input type="text" class="form-control" name="name" id="name" placeholder="Lütfen adınızı ve soyadınızı giriniz." required>
                    </div>
                    <div class="form-group">
                        <label for="email" class="control-label">Email</label>
                        <input type="email" class="form-control" name="email" id="email" placeholder="Email" data-error="Email adresi geçerli değil." required/>
                        </div>

                    <div class="form-group">
                        <div class="radio">
                            <label>
                                <input type="radio"  id ="isActive" name="radioActive" required>
                                Aktif
                            </label>
                        </div>
                        <div class="radio">
                            <label>
                                <input type="radio" id="isActive" name="radioActive" required>
                                Pasif
                            </label>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="groupID" class="control-label">Grup Adı</label>
                        <select class="form-control" name="groupID" id="groupID" required>
                            <option selected disabled  >Lütfen Grubunuzu seçiniz</option>
                            @foreach (var item in ViewData["Gruplar"] as IList<WebApplication1.Models.Groups>)
                            {
                                <option value="@item.id">@item.groupName</option>
                            }
                        </select>
                    </div>
                            <div class="form-group">
                                <button type="submit" class="btn btn-primary">Kullanıcıyı Kaydet</button>
                            </div>
                </form>
            </div>
        </div>

我正在使用MSSQL Server 17,电子邮件的数据类型是我的表中的varchar(50)。如何修复此问题并返回电子邮件不为空。我是编码和MVC5的新手,所以如果你能帮助我,我非常感谢。

c# sql-server html5 ef-code-first asp.net-mvc-5
1个回答
0
投票

对于“EntityValidationErrors”:您必须检查数据库表,表列的名称必须与视图元素名称完全相同(如果数据库表列“Email” - > view email element也必须是“Email”)。否则它将返回“EntityValidationErrors”。

© www.soinside.com 2019 - 2024. All rights reserved.