简体   繁体   中英

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

I just had help linking my model to my viewmodel in this controller - which seems to work. Here is the code:

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

        }

I have an issues with the code though:

if I load it up I get this error:

The parameters dictionary contains an invalid entry for parameter 'categoryId' for method '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 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]'. Parameter name: parameters

I am not familiar with the syntax being used to declare optional parameters in your TechSearchKnowledgebase method. Depending on what you are trying to do, try one of the following:

1) Remove the [Optional] tags. Your method would then look like this:

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

These are now nullable Guid parameters, and you could call this method as TechSearchKnowledgebase(null, null, null); Does this meet your needs?

2) If you truly require optional parameters, review Named and Optional Arguments . You can see therein that optional parameters are all declared after the required parameters, and that they all have a default value specified. Since you are using Guids, my guess is that you don't want these to truly be optional parameters, or that you would want to specify Guid.Empty as the default value. If the latter is true, your method definition would look like:

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

If I am misunderstanding your problem, please clarify and provide code where you are calling this method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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