C# asp net试图保存单个列,但我一直收到此错误-asp net MVC 5
当我试图在ASP NET MVC 5中编辑单个列时,不断出现此错误C# asp net试图保存单个列,但我一直收到此错误-asp net MVC 5,c#,sql,asp.net,asp.net-mvc,C#,Sql,Asp.net,Asp.net Mvc,当我试图在ASP NET MVC 5中编辑单个列时,不断出现此错误 Cannot insert the value NULL into column 'naam', table 这是我的邮政编码 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Index([Bind(Include = "id,email")] user user) { var username = HttpCo
Cannot insert the value NULL into column 'naam', table
这是我的邮政编码
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index([Bind(Include = "id,email")] user user)
{
var username = HttpContext.User.Identity.Name;
if (ModelState.IsValid)
{
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
这就是模型
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace LiemersApp.Models.EntityModels
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
public partial class profiel
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public profiel()
{
this.meldingens = new HashSet<meldingen>();
}
public int id { get; set; }
[DisplayName("Email:")]
public string email { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<meldingen> meldingens { get; set; }
}
}
//------------------------------------------------------------------------------
//
//此代码是从模板生成的。
//
//手动更改此文件可能会导致应用程序出现意外行为。
//如果重新生成代码,将覆盖对此文件的手动更改。
//
//------------------------------------------------------------------------------
命名空间LiemersApp.Models.EntityModels
{
使用制度;
使用System.Collections.Generic;
使用系统组件模型;
公共部分类profiel
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公共档案()
{
this.meldingens=new HashSet();
}
公共int id{get;set;}
[显示名称(“电子邮件:”)]
公共字符串电子邮件{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection meldingens{get;set;}
}
}
所以我想知道为什么它不起作用,因为我只想保存对电子邮件栏的更改任何想法为什么会发生这种情况,因为我无法弄清楚
问候语在您的示例中,您不会只更改模型中的name属性,而是映射控制器中出现并绑定的所有字段。如果不绑定某些属性,则
null
与错误字段类似
您可以这样更改控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index([Bind(Include = "id,email")] user user)
{
var username = HttpContext.User.Identity.Name;
if (ModelState.IsValid)
{
var userChange = db.profiels.Single(x => x.id == user.id);
userChange.email = user.Email;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
错误很明显,您需要在名称列中插入值。您显示的方法适用于class
user
,但您显示的模型适用于classprofiel
!显示正确的代码。