0

I have a bit of a weird issue with a record display. If a user enters a bar-code numeric (int) value into a URL, I'm supposed to show the details of the record linked to that bar-code. At one time, GUIDs (CodeId GUID PK) were used for the bar-codes, but now we are adding a new Int field (SerialId IDENTITY Int) to keep track of the actual bar-code value; we're not allowed to change the CodeId field on the table (which called Code), so this leads to my odd issue. I've looked around, and this QueryString id parameter not being used is the closest that I can find to addressing what's happening with me.

My Routing is perfectly normal

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

Model is normal

public DbSet<Code> Codes { get; set; }

public partial class Code
{
    public System.Guid CodeId { get; set; }
    public int SerialId { get; set; }
    public string Title { get; set; }
    public string BarCodeDescr { get; set; }
    public System.DateTime CreateDate { get; set; }
    public System.DateTime LastActivityDate { get; set; }
}

My controller worked just fine when things were a GUID, and the Id mapped to CodeId (the table's PK) etc.

    public ViewResult BarCode(Guid id)
    {
        Code code = db.Codes.Find(id);
        return View(code);
    }

Now I'm trying to do something more like this:

    public ViewResult BarCode(int serialid)
    {
        Code code = db.Codes.Find(serialid);
        return View(code);
    }

or this...

    public ActionResult QRCode(int serialid)
    {
        ViewData["SerialId"] = serialid;
        return View(db.Codes.Where(x => x.SerialId == serialid).ToList());
    }

...only all my attempts lead to the same error:

 The parameters dictionary contains a null entry for parameter 'serialid' 
 of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult 
 BarCode(Int32)' in 'MyProject.Controllers.HomeController'. An optional parameter 
 must be a reference type, a nullable type, or be declared as an optional parameter.
 Parameter name: parameters 

What am I doing wrong? It's not ideal to have my view return a record based on searching for a specific SerialId instead of the CodeId, but it should be do-able, right?

Community
  • 1
  • 1
edward_h
  • 171
  • 1
  • 2
  • 16

1 Answers1

1

You try using an optional parameter?

public ViewResult BarCode(int serialid = 0)
    {
        Code code = db.Codes.Find(serialid);
        return View(code);
    }
  • Hmm, no, that's a new idea to me. In trying it, I've got a different error: {"The type of one of the primary key values did not match the type defined in the entity. See inner exception for details.\r\nParameter name: keyValues"} {"The argument types 'Edm.Guid' and 'Edm.Int32' are incompatible for this operation. Near WHERE predicate, line 1, column 70."} ...which seems to be saying that it is still trying to return the view assuming that the value will go against CodeId/the GUID. – edward_h Apr 20 '12 at 23:26
  • Double check your database column type and maybe trying to refresh if you are using Entity Framework with a .edmx file – Christopher Rathermel Apr 21 '12 at 16:24
  • Ok, I double-checked and it is definitely SerialId (INT) with identity insert ON. And the .edmx is refreshed. No changes; I then tried public ViewResult BarCode(int? serialid = 0) with the ? after the int, and got a new error about properly identified PK values (which SerialId is not the PK, CodeId is.) {"The type of one of the primary key values did not match the type defined in the entity. See inner exception for details.\r\nParameter name: keyValues"} The inner exception is the same: where Edm.Guid and Edm.Int32 are not compatible. Is my ViewResult trying to make the PK go in the INT? – edward_h Apr 21 '12 at 21:39
  • Might want to look at a re-build or delete the files in the bin and obj folders or delete the .edmx file and re-add it. Creating a new project/solution w/ your code in isolation to see if there is another dependency that is causing the issue. – Christopher Rathermel Apr 24 '12 at 03:32
  • Christopher, thank you for all your help. I'm not certain that this is solvable the way that I'm going about it; I'll ask for permission to change the Id from a GUID to an INT, and directly address the issue. – edward_h Apr 24 '12 at 16:52