编辑和删除操作组合主键问题

本文关键字:问题 组合 操作 删除 编辑 | 更新日期: 2023-09-27 18:02:40

This my Edit Controller:

public ActionResult Edit(short id)
{
    TAUX taux = db.TAUX.Find(id);
    if (taux == null)
    {
        return HttpNotFound();
    }
    ViewBag.CAT_ID = new SelectList(db.CATEGORIE, "CAT_ID", "LIBELLE", taux.CAT_ID);
    ViewBag.C_GARANT = new SelectList(db.GARANTIE, "C_GARANT", "LIB_ABREGE", taux.C_GARANT);
    return PartialView("_Edit",taux);
}

在我的视图中是这样命名的:

@Html.ActionLink("Modifier", "Edit", new {  id=d.TAUX_ID })

这是我的模型Taux:

public partial class TAUX
{
    // the first 3 attributes are my primary key
    public short CAT_ID { get; set; } // foreign key
    public int C_GARANT { get; set; } // foreign key 
    public int TAUX_ID { get; set; }  
    [Required(ErrorMessage = "Taux est obligatoire")]
    public decimal POURC_TAUX { get; set; }
    public System.DateTime DATE_EFFET { get; set; }

    public virtual CATEGORIE CATEGORIE { get; set; }
    public virtual GARANTIE GARANTIE { get; set; }
}

我得到的错误是:

系统。ArgumentException:传递的主键值的数量必须匹配实体上定义的主键值的个数。

所以我试着这样做:

public ActionResult Edit(int taux_id ,int c_garant, short cat_id)
{
TAUX taux = db.TAUX.Find(taux_id, c_garant, cat_id); 
... }
//////////
@Html.ActionLink("Modifier", "Edit", new {  id=d.TAUX_ID, c_garant=d.C_GARANT, cat_id=d.CAT_ID })

但是我遇到了这个问题:

parameters字典包含一个空的parameter条目非空类型的taux_id。Int32' for方法'System.Web.Mvc.ActionResult Edit(Int32, Int32, Int16)' in"pfebs0.Controllers.TauxController"。可选参数必须为a引用类型、可空类型或声明为可选类型参数。

如何修复它(同样的问题deleteAction)?

编辑和删除操作组合主键问题

如果我没理解错的话。这三个属性是你的键,但不是你的主键,正如我在你的代码中看到的,你只是添加了注释来精确地说明CAT_IDC_GARANT是你的外键。关于第一次尝试。这意味着只有Taux_ID是主键。如果你想扩展方法Find工作,你需要精确的属性Key上面的属性,这是在你的表是主键。在您的例子中,您可以输入:

编辑

  public partial class TAUX
 {
    public int TAUX_ID { get; set; }  
    [Required(ErrorMessage = "Taux est obligatoire")]
    public decimal POURC_TAUX { get; set; }
    public System.DateTime DATE_EFFET { get; set; }
    public short CAT_ID { get; set; } // foreign key
    public virtual CATEGORIE CATEGORIE { get; set; }
    public int C_GARANT { get; set; } // foreign key 
    public virtual GARANTIE GARANTIE { get; set; }
   }

那么在Action中,不要修改:

    public ActionResult Edit(int id=0, int cat_Id =0,short c_garant=0)
   {
    TAUX taux = db.TAUX.Where(p=>p.TAUX_Id==id &&p.CAT_ID==cat_Id && p.C_GARANT==c_garant).FirstOrDefault();
    if (taux == null)
    {
        return HttpNotFound();
    }
    ViewBag.CAT_ID = new SelectList(db.CATEGORIE, "CAT_ID", "LIBELLE", taux.CAT_ID);
    ViewBag.C_GARANT = new SelectList(db.GARANTIE, "C_GARANT", "LIB_ABREGE", taux.C_GARANT);
    return PartialView("_Edit",taux);
    }

最简单的方法就是修改

public ActionResult Edit(int taux_id ,int c_garant, short cat_id)

public ActionResult Edit(int id ,int c_garant, short cat_id)

,

@Html.ActionLink("Modifier", "Edit", new {  id=d.TAUX_ID, c_garant=d.C_GARANT, cat_id=d.CAT_ID })

您在ActionLink中传递的Route值需要与action方法中的参数名称相匹配。

ActionLink更改为:

@Html.ActionLink("Modifier", "Edit", new { taux_id=d.TAUX_ID, c_garant=d.C_GARANT, cat_id=d.CAT_ID })