我有一个由View组成的ViewBag.People;
I have a ViewBag.People made up from a View;
var query = db.Vw_INTERACTPEOPLE.Select(p => new { p.PersonID, p.Fullname }); ViewBag.People = new SelectList(query.AsEnumerable(), "PersonID", "FullName");这一切都很好,但是在我看来,我还有另一个模型正在填充表格,并且其中一个项目是一个JobcontactID(文本框),它链接到PersonID(我没有设计数据库).因此,我想在视图袋中搜索ID,而不是显示ID,而是要显示人员的全名,那么是否有任何视图袋搜索功能?
Which all works fine, but in my view I have another model populating a table, and one of the items is populates is an JobcontactID (textbox) which links to the PersonID (i didnt design the database). So I want to search the viewbag for the ID and rather than displaying it I want to display the persons Fullname, so is there any viewbag search functionality?
推荐答案ViewBag 只是围绕 ViewData 的 dynamic 包装器(允许属性在运行时被调用,成为将用于在 ViewData 中查找值的键).您可以像这样查询 ViewData :
ViewBag is just a dynamic wrapper around ViewData (which allows the property invoked at runtime to become the key which will be used to look up the value in ViewData). You can query ViewData like this:
SelectList peopleSelectList = (from pair in ViewData where pair.Key == "People" select pair.Value);更新,那么您要查询选择列表本身吗?
Update So you wish to query the select list itself?
这是在剃刀视图中定义的功能:
Here's a function defined in the razor view:
@functions { public string FindPersonName(string id) { return (from item in ViewBag.People as SelectList where item.Value == id select item.Value).FirstOrDefault(); } } @functions.FindPersonName(jobContactId)更多推荐
有没有办法在ViewBag中搜索项目?
发布评论