Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将两个或多个类模型导入ASP.NET上的单个控制器_Asp.net_Database_Import_Controller - Fatal编程技术网

将两个或多个类模型导入ASP.NET上的单个控制器

将两个或多个类模型导入ASP.NET上的单个控制器,asp.net,database,import,controller,Asp.net,Database,Import,Controller,我对Java非常陌生,但我懂一点Java编程。我想使用查询一个将返回字符串的数据库,然后使用该字符串查询另一个数据库。我想在同一个控制模型上做这个。我以为这很容易,听起来也很容易 当我创建控制器时,我放置了第一个数据库的模型类,到目前为止,我已经查询了第一个数据库,但是现在我有了字符串,我想通过DBEntities查询第二个数据库 这将显示一个错误,说明: > The model item passed into the dictionary is of type > 'System

我对Java非常陌生,但我懂一点Java编程。我想使用查询一个将返回字符串的数据库,然后使用该字符串查询另一个数据库。我想在同一个控制模型上做这个。我以为这很容易,听起来也很容易

当我创建控制器时,我放置了第一个数据库的模型类,到目前为止,我已经查询了第一个数据库,但是现在我有了字符串,我想通过DBEntities查询第二个数据库

这将显示一个错误,说明:

> The model item passed into the dictionary is of type
> 'System.Collections.Generic.List`1[FinalBallot.Models.AgainCandidate]',
> but this dictionary requires a model item of type
> 'System.Collections.Generic.IEnumerable`1[FinalBallot.Models.ZipTable]'.
有没有一种简单的方法来解决这个问题

public class Default1Controller : Controller
{
    private CandidatesDBEntities db = new CandidatesDBEntities();

    public string districString = "";
    //
    // GET: /Default1/

    public ViewResult Index(string searchString)
    {
        var queryZip = from s in db.ZipTables select s;
        var queryCandidates = from s1 in db.AgainCandidates select s1;
        double sT = 0;

        //method so it doesnt display the whole db
        if (String.IsNullOrEmpty(searchString))
        {
            queryZip = queryZip.Where(s => s.ZipL.Equals(0));
        }

        if (!String.IsNullOrEmpty(searchString))
        {
            sT = double.Parse(searchString);
            queryZip = queryZip.Where(s => s.ZipL.Equals(sT));

            try
            {
                districString = queryZip.ToList().ElementAt(0).District;
            }
            catch
            {
            }

            if (!String.IsNullOrEmpty(districString))
            {
                queryCandidates = queryCandidates.Where(s1 => s1.District.Equals(districString));
            }
        }
        return View(queryCandidates.ToList());
    }

在您看来,您是否将模型指定为
IEnumerable
?传递给视图的模型是
IEnumerable
,因此如果将模型指定为其他对象,则会出现错误。您需要将视图中的模型更改为
IEnumerable

更新: 根据修改后的解释,您可以做以下几件事: 1) 为要在页面上显示的每个集合创建一个具有两个属性的“ViewModel”,如下所示:

public class MyViewModel
{
    IEnumerable<ZipTable> Zips { get; set; }
    IEnumerable<AgainCandidate> Candidates { get; set; }
}
您可以按如下方式在视图中提取此数据:

@foreach (var zip in ViewData["Zips"] as IEnumerable<ZipTable>) 
{
    ...
}
@foreach(ViewData[“Zips”]中的var zip作为IEnumerable)
{
...
}

它被指定为ZipTable,因为第一个查询使用的是IEnumerable,但我想进行第二个查询并显示第二个表的视图“IEnumerable”,我想如果我改为againcadate,它将显示一个错误,因为它无法识别ZipTable。根据您的反馈更新了帖子。谢谢您的回答我将尝试实现它,因为我不太熟悉,所以我开始阅读ViewModel以及如何实现第一个解决方案,谢谢
@foreach (var zip in ViewData["Zips"] as IEnumerable<ZipTable>) 
{
    ...
}