繁体   English   中英

使用Guid作为可选参数给出“参数需要类型为'System.Nullable'的值”

[英]Using Guid as an optional parameter gives 'parameter requires a value of type 'System.Nullable'

我只是在将我的模型链接到该控制器中的视图模型方面获得了帮助-似乎可行。 这是代码:

public ActionResult TechSearchKnowledgebase([Optional]Guid? createdById, [Optional]Guid? categoryId, [Optional]Guid? typeId)
        {

            var model = db.Knowledgebases.AsQueryable();

            if (createdById != Guid.Empty)
            {
                model = model.Where(k => k.CreatedById == createdById);
                ViewBag.CreatedBy = db.Users.Where(c => c.UserId == createdById).First().FullName;
            }
            if (categoryId != Guid.Empty)
            {
                model = model.Where(k => k.CategoryId == categoryId);
                ViewBag.Category = db.Categories.Where(c => c.CategoryId == categoryId).First().CategoryName;
            }
            if (typeId != Guid.Empty)
            {
                model = model.Where(k => k.TypeId == typeId);
                ViewBag.Category = db.Roles.Where(c => c.RoleID == typeId).First().RoleDescription;
            }
            model=model.OrderBy(k => k.CreatedDate);

            List<KnowledgebaseResult> knowledgebaseResults = Mapper.Map<List<KnowledgebaseResult>>(model.ToList());

            return View("TechKnowledgebaseList", knowledgebaseResults);

        }

我的代码有问题:

如果我加载它,我得到这个错误:

参数字典包含方法'System.Web.Mvc.ActionResult TechSearchKnowledgebase(System.Nullable 1[System.Guid], System.Nullable 1 [System.Guid],System.Nullable 1[System.Guid])' in 'HelpDesk.WebUI.Controllers.KnowledgebaseController'. The dictionary contains a value of type 'System.Reflection.Missing', but the parameter requires a value of type 'System.Nullable ]的参数'categoryId'的无效条目1[System.Guid])' in 'HelpDesk.WebUI.Controllers.KnowledgebaseController'. The dictionary contains a value of type 'System.Reflection.Missing', but the parameter requires a value of type 'System.Nullable 1[System.Guid])' in 'HelpDesk.WebUI.Controllers.KnowledgebaseController'. The dictionary contains a value of type 'System.Reflection.Missing', but the parameter requires a value of type 'System.Nullable 1 [System.Guid]”的值。 参数名称:参数

我不熟悉用于在TechSearchKnowledgebase方法中声明可选参数的语法。 根据您要执行的操作,请尝试以下操作之一:

1)删除[可选]标签。 您的方法将如下所示:

TechSearchKnowledgebase(Guid? createdById, Guid? categoryId, Guid? typeId)

这些现在是可为空的Guid参数,您可以将此方法称为TechSearchKnowledgebase(null, null, null); 这符合您的需求吗?

2)如果您确实需要可选参数,请查看命名和可选参数 您可以在其中看到所有可选参数都在必需参数之后声明,并且它们都指定了默认值。 由于您使用的是Guid,因此我猜测您不希望它们真正成为可选参数,或者您希望将Guid.Empty指定为默认值。 如果后者为true,则您的方法定义将如下所示:

public ActionResult TechSearchKnowledgebase(Guid? createdById = Guid.Empty, Guid? categoryId = Guid.Empty, Guid? typeId = Guid.Empty)

如果我误解了您的问题,请在调用此方法的地方进行澄清并提供代码。

暂无
暂无

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

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