尝试执行插入时收到以下错误:
无法将值 NULL 插入列“Request_Cat_ID”、表“dbo.Request_Categories”;列不允许空值。插入失败。
这个错误让我感到困惑的原因是因为我有一个非常相似的表设置,其中键列是字符串而不是 int。我可以毫无问题地插入该表。插件小而简单,所以这里是细分:
模型:
[Table("Request_Categories")]
public class RequestCategory
{
[Key]
[Column("Request_Cat_ID")]
public int RequestCategoryID { get; set; }
[Column("Request_Category")]
public string RequestCategoryName { get; set; }
}
视图模型:
public class RequestCategoryViewModel
{
public IEnumerable<RequestCategory> RequestCategories { get; set; }
}
控制器方法:
[HttpPost]
public JsonResult AddRequestCategory(RequestCategory model)
{
var foo = model.RequestCategoryID; //There is a value that appears here. It does have an int type.
db.RequestCategories.Add(model);
db.SaveChanges();
return Json("");
}
从视图传入的位置:
$.post('@Url.Action("AddRequestCategory", "Services", new { area = "services" })',
{
RequestCategoryID: form.currentTarget.RequestCategoryID.value,
RequestCategoryName: form.currentTarget.RequestCategoryName.value,
}, AddRow, "json");
欢迎任何建议或提示!