有没有办法在ViewBag中搜索项目? [英] Is there a way to search for an item in a ViewBag?

查看:85
本文介绍了有没有办法在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中搜索项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆