سؤال

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 :

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.

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'. 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)?

هل كانت مفيدة؟

المحلول

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. Regarding the first try. Meaning only Taux_ID is your Primary Key. 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. 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:

    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.

Change your ActionLink to this:

@Html.ActionLink("Modifier", "Edit", new { taux_id=d.TAUX_ID, c_garant=d.C_GARANT, cat_id=d.CAT_ID })
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top