扩展方法不能动态调度 [英] Extension methods cannot be dynamically dispatched
问题描述
我想有DropDownListFor在MVC
i want to have DropDownListFor in MVC
@foreach (var item in Model)
{
@Html.DropDownListFor(modelItem => item.TitleIds, new SelectList(ViewBag.TitleNames as System.Collections.IEnumerable, "TitleId", "Title.TitleText"), "No: " + ViewBag.MagNo, new { id = "TitleIds" })
}
在控制器
public ActionResult ArticleList(int id)
{
ArticleWriter_ViewModel viewModel = new ArticleWriter_ViewModel();
Func<IQueryable<NumberTitle>, IOrderedQueryable<NumberTitle>> orderByFunc = null;
Expression<Func<NumberTitle, bool>> filterExpr = null;
if (id > 0)
{
filterExpr = p => p.MagazineId.Equals(id);
}
var wholeTitles = unitOfWork.NumberTitleRepository.Get(filterExpr, orderByFunc, "Magazine,Title").ToList();
ViewBag.MagNo = wholeTitles[0].Magazine.MagNo.ToString();
ViewBag.MagId = wholeTitles[0].Magazine.Id;
ViewBag.TitleNames = wholeTitles;
return View("../Panel/Magazine/ArticleList", "_BasicLayout", viewModel);
}
但我得到这个错误
but i get this error
'<$c$c>System.Web.Mvc.HtmlHelper<System.Collections.Generic.IEnumerable<Cinemavaadabiat.ViewModel.ArticleWriter_ViewModel>>$c$c>'有一个名为 DropDownListFor
'不适用的方法,但似乎有这个名字的扩展方法。扩展方法不能进行动态调度。考虑强制转换动态参数或调用没有扩展方法的语法扩展方法。
'
System.Web.Mvc.HtmlHelper<System.Collections.Generic.IEnumerable<Cinemavaadabiat.ViewModel.ArticleWriter_ViewModel>>
' has no applicable method named 'DropDownListFor
' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.
我上无法理解错误,应该检查呢?
I cann't understand the error, what should check for it?
推荐答案
您在扩展方法使用动态类型,这是不支持的。
You are using dynamic types in extension methods, which is not supported.
铸造动态类型的实际类型,并且它会工作。
Cast the dynamic types to actual types, and it will work.
从我现在看到的,我会说:
From what I see now, I'd say:
(string) ViewBag.MagNo
这将导致
@foreach (var item in Model)
{
@Html.DropDownListFor(modelItem => item.TitleIds,
new SelectList(ViewBag.TitleNames as System.Collections.IEnumerable,
"TitleId", "Title.TitleText"),
"No: " + (string) ViewBag.MagNo,
new { id = "TitleIds" })
}
这篇关于扩展方法不能动态调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!