有没有办法在ViewBag中搜索项目?

编程入门 行业动态 更新时间:2024-10-25 10:26:47
本文介绍了有没有办法在ViewBag中搜索项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个由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中搜索项目?

本文发布于:2023-10-28 13:36:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536785.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:没有办法   项目   ViewBag

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!