本文介绍了我们可以在MVC中查看LINQ - 查看页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



我从控制器传递两个列表,通过ViewBag查看。



在视图中,我想使用linq查询。 >>我可以这样做吗?



如果是,请回复示例。



问候

YRISHI

Dear All,

I'am passing two list from controller to view via "ViewBag".

In view, I want to use linq query. >> Can I do this in view??

If yes, Please revert with example.

Regards
YRISHI

推荐答案

@var data = ViewData["YourData"] as List<yourentityclass>;
   foreach (var item in data.Where(x=>x.Id==1))
   { 
    //html
   } 



希望这有助于


Hope this helps


You need to import the namespace into your view by adding @using System.Data.Linq at the top of your view. However if you want it in all your views then you need to add <add namespace="System.Data.Linq" /> to the web.config in your Views folder:

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Data.Linq" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>
Although not relevent to your question you should really try to move this logic out of the view and into the controller, it will make things much easier to debug and means that your presentation is separated from your business logic.


这篇关于我们可以在MVC中查看LINQ - 查看页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 04:27