0

我对记录显示有一点奇怪的问题。如果用户在 URL 中输入条形码数字 (int) 值,我应该显示链接到该条形码的记录的详细信息。曾经,条形码使用 GUID (CodeId GUID PK),但现在我们添加了一个新的 Int 字段 (SerialId IDENTITY Int) 来跟踪实际的条形码值;我们不允许更改表上的 CodeId 字段(称为 Code),所以这导致了我的奇怪问题。我环顾四周,这个未使用的 QueryString id 参数是我能找到的最接近解决发生在我身上的事情的方法。

我的路由完全正常

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

型号正常

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

当事物是 GUID 并且 Id 映射到 CodeId(表的 PK)等时,我的控制器工作得很好。

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

现在我正在尝试做更多这样的事情:

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

或这个...

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

...只有我所有的尝试都会导致同样的错误:

 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 

我究竟做错了什么?让我的视图根据搜索特定 SerialId 而不是 CodeId 返回记录并不理想,但它应该是可行的,对吧?

4

1 回答 1

1

您尝试使用可选参数吗?

public ViewResult BarCode(int serialid = 0)
    {
        Code code = db.Codes.Find(serialid);
        return View(code);
    }
于 2012-04-20T22:55:21.073 回答