gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:39:02 29 4
gpt4 key购买 nike

首先,我首先使用数据库 MVC5/C# 中的 Entity Framework 代码。我想添加新用户,但是当我尝试添加电子邮件时出现错误。

这是我的 Controller 方法:

      [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 新手,所以如果你能帮助我,我将非常感激。

最佳答案

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

关于c# - 一个或多个实体的验证失败。有关源错误 db.SaveChanges() 的更多详细信息,请参阅 'EntityValidationErrors' 属性;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45477548/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com