出现重复错误违反PRIMARY KEY约束并被卡住

时间:2019-07-11 09:53:59

标签: asp.net-mvc viewmodel dbcontext

我试图用一个视图获取一个表格,而我却得到了2个表,而我试图用第二个关系来获取2.table。我正在获取id的客户选择。但是当我试图用其他表插入post方法时给我错误。

我的personalId总是为0。插入方法是在保存更改后添加数据库。

这是我的控制器:

     [HttpGet]
     public ActionResult Add()
     {
        FormDetailViewModel model = new FormDetailViewModel();
        List<SelectListItem> FormList = new List<SelectListItem>();
        List<SelectListItem> İtemList = new List<SelectListItem>();
        List<SelectListItem> TypeList = new List<SelectListItem>();


        foreach(Form item in formRep.List().ProcessResult)
        {
            FormList.Add(new SelectListItem { Value = 
   item.FormId.ToString(), Text = item.TeslimEden });
        }
        foreach(Item item in itemRep.List().ProcessResult)
        {
            İtemList.Add(new SelectListItem { Value = 
  item.ItemId.ToString(), Text = item.ItemDesc });
        }
        foreach(CheckListType item in typeRep.List().ProcessResult)
        {
            TypeList.Add(new SelectListItem { Value = 
  item.CheckListTypeId.ToString(), Text = item.CheckListType1 });
        }
        model.checkLists = TypeList;
        model.FormViewList = FormList;
        model.İtems = İtemList;

        return View(model);
    }
    [HttpPost]
    public ActionResult Add(FormDetailViewModel model)
     {
       Personal personal = new Personal();
        personal.PersonalAdı = model.PersonalName;
        personal.PersonalDepartmanı = model.PersonalDepartment;
        personal.IseGırısTarihi = model.IseGirisTarihi;
        personal.PersonalId = Convert.ToInt32(model.PersonId);
        personalRep.Insert(personal);
        fDetailRep.Insert(model.FormDetail);
        return View(model);


     }

这是我的模特:

    public FormDetail FormDetail { get; set; }
    public IEnumerable<SelectListItem> FormViewList { get; set; }
    public IEnumerable<SelectListItem> checkLists { get; set; }
    public IEnumerable<SelectListItem> İtems { get; set; }
    [Display(Name = "PersonalAdı")]
    public string PersonalName { get; set; }
    [Display(Name = "PersonalDepartmanı")]
    public string PersonalDepartment { get; set; }
    [Display(Name = "IseGirisTarihi")]
    public string IseGirisTarihi { get; set; }
    [Key]
    public int PersonId { get; set; }

查看:

@model TodoListApp.Areas.User.Models.ViewModel.FormDetailViewModel
@using (Html.BeginForm())
{
<table class="table table-hover">
    <tr>
        <td>
            @Html.Label("PersonalAdı")
        </td>
        <td>@Html.EditorFor(model => model.PersonalName)</td>

    </tr>

    <tr>
        <td>
            @Html.DisplayNameFor(model => model.FormDetail.Form)
        </td>
        <td>
            @Html.DropDownListFor(model => model.FormDetail.FormId, 
  Model.FormViewList)
        </td>
    </tr>
    <tr>
        <td>
            @Html.DisplayNameFor(model => model.FormDetail.CheckListType)
        </td>
        <td>
            @Html.DropDownListFor(model => 
 model.FormDetail.CheckListTypeId, Model.checkLists, new { @onchange = 
 "createlement()" })
        </td>
    </tr>
    <tr>
        <td colspan="2" style="padding-top:40px;">
            <input type="submit" value="Save" class="btn btn-info" />
        </td>
    </tr>
</table>

}

SqlException:违反PRIMARY KEY约束“ PK_FormDetail”。不能  在对象“ dbo.FormDetail”中插入重复键。重复的键值为  (0)。  该声明已终止。

0 个答案:

没有答案