首页 > 解决方案 > 使用 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;}

期望将记录保存到数据库中。应用程序不会崩溃。

标签: c#asp.net-mvc-5kendo-asp.net-mvc

解决方案


推荐阅读