![](/img/trans.png)
[英]The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>'
[英]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.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
]的參數'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.