如何访问收集队列< T>在MVC中从控制器传递到视图 [英] How to access a collection queue<T> passing from controller to view in MVC

查看:47
本文介绍了如何访问收集队列< T>在MVC中从控制器传递到视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



以下是我模特中的课程

  public   class  LuceneSearchConfig 
{
public SearchResultObject getResults;

public class SearchResultObject
{
public 队列< List< string>>数据{获取; set ; }
}
}



------------------------ ---------

以下是我的控制器:

测试actionresult run()
{
DCS.getResults = new LuceneSearchConfig.SearchResultObject();

DCS.getResults = DCS.SearchIndexWithTermQuery(xsearchtext,xtmmodel, 5 );

return PartialView( DocSearchResults,DCS.getResults);
}



------------------------------ ---

以下是我的视图:(由于数据类型错误,它不会打开)。请参阅下面的错误消息。)

 @ model IEnumerable <   nowmvc.models.lucenesearchconfig.searchresultobject  >  

@foreach(模型中的var项目)
{
//阅读@ item.data
}



请帮助我如何将正确的类型发送到VIEW屏幕。谢谢。



我尝试了什么:



我试过将其转换为数组但不起作用。我也尝试将DCS.getResults转换为IEnumerable并收到以下错误:



Quote:

传入字典的模型项的类型为'System.Collections.Generic.Queue`1 + Enumerator [System.Collections.Generic.List`1 [System.String]]',但此字典需要一个模型'System.Collections.Generic.IEnumerable`1

解决方案

类型的项目以下是您正在尝试做的工作模拟:



1.模型

 使用 System.Collections.Generic; 

命名空间 MvcQueue.Models
{
public class DataModel
{
public Queue< List< string>>数据{获取; set ; }
= new 队列< List< string>>();
}
}

2。控制器:

 使用 MvcQueue.Models; 
使用 System.Collections.Generic;
使用 System.Web.Mvc;

命名空间 MvcQueue.Controllers
{
public class HomeController:Controller
{
public ActionResult Index()
{
var results = new DataModel();
for int i = 0 ; i < 10 ; i ++)
{
results.Data .Enqueue( new List< string>(){ aaa bbb ccc});
}
return 查看(results.Data);
}
}
}

3。查看:

 @ {
ViewBag.Title =队列数据测试;
}
< div >
@foreach(模型中的var项目)
{
< p > 数据:
@ string.Join(,,item);
< / p >
}
< / div >

如果您需要在视图中声明模型

 @ Model = IEnumerable <  列表< string  > 取代; 


Hello,

Below is the class in my models

public class LuceneSearchConfig
{
  public SearchResultObject getResults;

      public class SearchResultObject 
        {
            public Queue<List<string>> data { get; set; }
        }
}


---------------------------------
Below is my controller:

test actionresult run()
{
  DCS.getResults = new LuceneSearchConfig.SearchResultObject();

DCS.getResults = DCS.SearchIndexWithTermQuery(xsearchtext, xtmmodel, 5);

 return PartialView("DocSearchResults", DCS.getResults);
}


---------------------------------
Below is my VIEW: (it won't open due to the wrong data type). Please see the error message below.)

@model IEnumerable<nowmvc.models.lucenesearchconfig.searchresultobject>

@foreach (var item in Model)
 {
// reading @item.data
}


Please help me how to send the correct type to the VIEW screen. Thank you.

What I have tried:

I tried to convert it to array but that won't work. I also tried casting "DCS.getResults" as IEnumerable and received errors below:

Quote:

The model item passed into the dictionary is of type 'System.Collections.Generic.Queue`1+Enumerator[System.Collections.Generic.List`1[System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1

解决方案

Here is a working mock of what you are trying to do:

1. Model

using System.Collections.Generic;

namespace MvcQueue.Models
{
    public class DataModel
    {
        public Queue<List<string>> Data { get; set; } 
            = new Queue<List<string>>();
    }
}

2. Controller:

using MvcQueue.Models;
using System.Collections.Generic;
using System.Web.Mvc;

namespace MvcQueue.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var results = new DataModel();
            for (int i = 0; i < 10; i++)
            {
                results.Data.Enqueue(new List<string>() { "aaa", "bbb", "ccc" });
            }
            return View(results.Data);
        }
    }
}

3. View:

@{
    ViewBag.Title = "Queue Data Test";
}
<div>
    @foreach (var item in Model)
    {
        <p> data: 
        @string.Join(", ", item);
        </p>
    }
</div>

If you need to declare the Model in the view:

@Model = IEnumerable<List<string>>;


这篇关于如何访问收集队列&lt; T&gt;在MVC中从控制器传递到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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