简体   繁体   English

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

[英]Edit and Delete Action composite primary keys issue

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);
}

And this is how I call it in my view : 这就是我在观点中称之为:

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

This my model Taux : 这是我的模型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; }
}

And this the Error I get : 这就是我得到的错误:

system.ArgumentException: The number of primary key values passed must match number of primary key values defined on the entity. system.ArgumentException:传递的主键值的数量必须与实体上定义的主键值的数量相匹配。

So I tried to do this : 所以我试着这样做:

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 })

but I got this problem : 但我遇到了这个问题:

The parameters dictionary contains a null entry for parameter 'taux_id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(Int32, Int32, Int16)' in 'pfebs0.Controllers.TauxController'. 参数字典包含'pfebs0.Controllers.TauxController'中方法'System.Web.Mvc.ActionResult Edit(Int32,Int32,Int16)'的非可空类型'System.Int32'的参数'taux_id'的空条目。 An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. 可选参数必须是引用类型,可空类型,或者声明为可选参数。

How to fix it (Same issue for deleteAction)? 如何解决(deleteAction的相同问题)?

If I understand well your problem. 如果我理解你的问题。 The three properties are your keys but not your primary keys as I saw in your code you just added comments to precise that the CAT_ID and C_GARANT are your foreign keys. 这三个属性是您的键,但不是您的主键,因为我在您的代码中看到您刚刚添加注释以确切地说CAT_IDC_GARANT是您的外键。 Regarding the first try. 关于第一次尝试。 Meaning only Taux_ID is your Primary Key. 仅含义Taux_ID是您的主键。 If you want the extension method Find work you need to precise the attribute Key above the property which is in your table is the primary key. 如果你想要扩展方法Find工作,你需要精确的属性Key上面的属性是主键。 In your case you could have: 在你的情况下,你可以:

Edit 编辑

  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; }
   }

Then in your Action, don't change: 然后在你的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);
    }

Simplest thing to try is to change 最简单的尝试就是改变

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

into

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

and leave 然后离开

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

as is now 就像现在一样

The Route values you pass in the ActionLink need to match the name of the parameters in the action method. 您在ActionLink传递的Route值需要与action方法中的参数名称匹配。

Change your ActionLink to this: ActionLink更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM