c# - 使用 kendoUI 将表单值发布到 DB
问题描述
无法将记录保存到数据库
我尝试了将值传递给模型的不同方法,甚至是硬编码值。
模型:
public class RaarrInspectionModel : BaseModel
{
public int InspectionTypeId { get; set; }
public int ProcedureId { get; set; }
public int SpecificationId { get; set; }
public int ResultId { get; set; }
public int RaarrEquipmentId { get; set; }
public DateTime InspectionDate { get; set; }
public DateTime NextInspectionDue { get; set; }
public int InspectorId { get; set; }
public string Note { get; set; }
public bool ReadManufacturerInfo { get; set; }
public bool RecomendedLifespan { get; set; }
public bool LoadLimitCheck { get; set; }
public bool VisualTactileCheck { get; set; }
public bool FuntionChecks { get; set; }
}
视图模型:
public class RaarrInspectionVm : BaseModel
{
public int Id { get; set; }
public int InspectionTypeId { get; set; }
public int ProcedureId { get; set; }
public int SpecificationId { get; set; }
public int? RaarrEquipmentId { get; set; }
public int ResultId { get; set; }
public DateTime InspectionDate { get; set; }
public DateTime NextInspectionDue { get; set; }
public int InspectorId { get; set; }
public string Note { get; set; }
public bool ReadManufacturerInfo { get; set; }
public bool RecomendedLifespan { get; set; }
public bool LoadLimitCheck { get; set; }
public bool VisualTactileCheck { get; set; }
public bool FuntionChecks { get; set; }
}
视图非常简单,使用 kendo d-down 列表和复选框
控制器:
public ActionResult Inspect()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public virtual ActionResult Inspect(RaarrInspectionVm vm)
{
var model = vm.ToModel(new RaarrInspectionModel());
var raarrInspectionEf = new RaarrInspectionEf();
raarrInspectionEf.Upsert(model);
return RedirectToAction("Equipment");
}
实体框架类:
public RaarrInspection Upsert(RaarrInspectionModel model)
{
try
{
return model.Id.ValidId() ? Update(model) : Add(model);
}
catch (Exception ex){
new LogError(false, "Code RaarrEquipment",
MethodBase.GetCurrentMethod(), Environment.UserName, ex,
model).LogToEventLog();
return null;}}
private RaarrInspection Add(RaarrInspectionModel model)
{var c = model.ToDomain(new RaarrInspection());
_db.RaarrInspection.Add(c);
_db.SaveChanges();
return c;}
期望将记录保存到数据库中。应用程序不会崩溃。
解决方案
推荐阅读
- java - quarkus 启动失败并出现 AppModel 初始化异常
- android - 使用“慢速测试卡。几分钟后拒绝”的 Android BillingClient 购买仍处于待处理状态
- android - 使用导航组件的多个后退堆栈 - 如何始终转到特定底部导航选项卡的起始目的地?
- snakemake - InputFunctionException - 扩展和输入函数未解析通配符
- javascript - 为什么我不能在主要功能中编写 console.log
- c# - 是否可以不绑定到元素的属性,而是绑定到 WPF 中的元素本身?
- node.js - 从 Heroku 向 Firebase 进行身份验证错误:找不到模块
- php - 替换数组中所有在php中具有“o”字母的值
- amazon-web-services - Amplify (AWS) 验证电子邮件给出 400 错误请求错误
- javascript - 关于对象上的 ES6 扩展运算符的快速问题