Je viens d'avoir de l'aide pour lier mon modèle à mon viewmodel dans ce contrôleur - ce qui semble fonctionner. Voici le code:L'utilisation de Guid en tant que paramètre optionnel donne 'paramètre nécessite une valeur de 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);
}
J'ai un problème avec le code si:
si je le charger j'obtiens cette erreur:
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.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.Nullable
1[System.Guid]'. Parameter name: parameters
Copie possible de [Comment puis-je par défaut un paramètre à Guid.Empty en C#?] (Https://stackoverflow.com/questions/5117970/how-can-i-default-a-parameter-to-guid- vide-en-c) –