0

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.Nullable1[System.Guid], System.Nullable1[System.Guid], System.Nullable1[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.Nullable1[System.Guid]'. Parameter name: parameters

djblois
  • 963
  • 1
  • 17
  • 52
  • Possible duplicate of [How can I default a parameter to Guid.Empty in C#?](https://stackoverflow.com/questions/5117970/how-can-i-default-a-parameter-to-guid-empty-in-c) – Michael Freidgeim Nov 19 '17 at 11:48

1 Answers1

-1

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.

Bobby
  • 467
  • 4
  • 13