简体   繁体   English

如何检查ViewBag中的列表是否包含字符串

[英]How to check if a List in a ViewBag contains string

I have a ViewBag that is a list which i create in the controller: 我有一个ViewBag,它是我在控制器中创建的列表:

 List<string> listoflists = new List<string>();

it then gets filled with some string like for example: 然后,它会填充一些字符串,例如:

listoflists.Add("java");
listoflists.Add("php");
listoflists.Add("C#");

Adding it to the ViewBag: ViewBag.listoflists = listoflists; 将其添加到ViewBag:ViewBag.listoflists = listoflists;

i then would like to check in my View if this contains a specific string: 然后,我想检查一下我的视图是否包含特定的字符串:

@if(ViewBag.listoflists.contains("java")
{
// do html stuff
}

but when running this i get the following error: 但是当运行这个我得到以下错误:

System.Collections.Generic.List does not contain a definition for contains System.Collections.Generic.List不包含包含的定义

What am i doing wrong, or how should i check if a list contains something in the View? 我在做什么错,或者我应该如何检查列表中是否包含某些内容?

You might need to cast back to a list of strings: 您可能需要回退到字符串列表:

@if (((IList<string>)ViewBag.listoflists).Contains("java")
{
    // do html stuff
}

Also notice that the Contains method starts with a capital C . 还要注意, Contains方法以大写字母C开头。

So as you can see using ViewBag in ASP.NET MVC is a complete crap leading to very ugly code in your views. 如您所见,在ASP.NET中使用ViewBag MVC是一个完全废话,导致视图中的代码非常丑陋。 For this reason it is strongly recommended to use view models which will contain all the necessary information of a specific view and also being strongly typed. 因此,强烈建议使用视图模型,该模型将包含特定视图的所有必要信息,并且也应进行强类型化。

So cut this ViewBag crap and start using view models: 因此,请剪切此ViewBag废话并开始使用视图模型:

public class MyViewModel
{
    public IList<string> ListOfLists { get; set; }
}

that your controller action can populate and pass to the view: 您的控制器动作可以填充并传递给视图:

public ActionResult Index()
{
    var model = new MyViewModel();
    List<string> listoflists = new List<string>();
    listoflists.Add("java");
    listoflists.Add("php");
    listoflists.Add("C#");
    model.ListOfLists = listoflists;
    return View(model);
}

and now you can have a strongly typed view to this model which will allow you to avoid the previous casting: 现在您可以对该模型有一个强类型的视图,这将使您避免以前的转换:

@model MyViewModel

@if (Model.ListOfLists.Contains("java"))
{
    // do html stuff
}

So basically every time you use ViewBag/ViewData in an ASP.NET MVC application an alarm should immediately ring in your head telling you: Man, what the hell, you are doing it wrong. 因此,基本上,每次在ASP.NET MVC应用程序中使用ViewBag / ViewData时,都会立即响起警报,告诉您:伙计,这到底是什么,您做错了。 Just use a view model in order to avoid transforming your views into an abominable mess of completely irrelevant C# language constructs like casting and stuff. 只需使用视图模型即可避免将视图转换为完全不相关的C#语言构造(如类型转换和填充)的可恶的混乱。 Views are meant for displaying markup. 视图用于显示标记。

ViewBag is a dynamic type, it does not know at compile time what is the actual type contained in the key ViewBag.listoflists you defined, you need to cast it to specific type and then you can call those methods on it: ViewBag是动态类型,它在编译时不知道您定义的键ViewBag.listoflists包含的实际类型是什么,您需要将其ViewBag.listoflists为特定类型,然后可以在其上调用这些方法:

@{

List<string> languages= ViewBag.listoflists as List<string>;
}
@if(languages.contains("java")
{
// do html stuff
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM