c# - 无法将类型 'System.Linq.IQueryable<>' 隐式转换为 'Model' 。存在显式转换(是否缺少强制转换?)

标签 c# asp.net-mvc asp.net-mvc-4 listbox

我是MVC新手,我对MVC了解不多,所以有时我无法得到错误, 我正在创建双列表框并将 listbox1 的项目移动到 listbox2,在第一个列表框中从数据库中获取数据,因此我创建了以下内容。 模型.cs

public class Model
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Class { get; set; }
}

ViewModel.cs

public class ViewModel
{
    public List<Model> AvailableModel { get; set; }

    public List<Model> RequestedModel { get; set; }

    public string[] AvailableSelected { get; set; }
    public string[] RequestedSelected { get; set; }
}

在 Controller 中从数据库获取数据我创建了以下内容

[NonAction]
private Model getName()
{
    var name = (from m in db.Models
                select m);
    return name;
}

[NonAction]
public List<Model> getAllInstituteNameList()
{
    return getName();
}

[HttpGet]
public ActionResult Index()
{
    ViewModel model = new ViewModel { AvailableModel = getAllInstituteNameList(), RequestedModel = newList<Model>() };
    return View();
}

运行后,抛出以下异常

Cannot implicitly convert type 'System.Linq.IQueryable' to 'DemoListBox.Models.Model'. An explicit conversion exists (are you missing a cast?)

在这条线上

var name = (from m in db.Models
    select m);
    return name;

请帮忙解决一下......

已更新

按照你的答案后.. 我再次看到这个新的异常

Object reference not set to an instance of an object.

在这一行

<%:Html.ListBoxFor(model =>model.AvailableSelected, newMultiSelectList(Model.AvailableModel, "Id", "Name", Model.AvailableSelected))%>

  • 已编辑

查看

<h2>Index</h2>
<%using(Html.BeginForm()) {%>
<div>
<hr/>
<table>
<thead>
<tr>
<th>
                       Available
</th>

<th>

</th>

<th>
                       Requested
</th>
</tr>
</thead>

<tbody>
<tr>
<td>
<%:Html.ListBoxFor(model =>model.AvailableSelected, newMultiSelectList(Model.AvailableModel, "Id", "Name", Model.AvailableSelected))%>
</td>

<td>
<inputid="add"name="add"type="submit"value=">>"/>
<br/>
<inputid="remove"name="remove"type="submit"value="<<"/>
</td>

<td>
<%:Html.ListBoxFor(model=>model.RequestedSelected,newMultiSelectList(Model.RequestedModel,"Id","Name",Model.RequestedSelected)) %>
</td>
</tr>
</tbody>
</table>
<br/>

<hr/>
</div>
<% }%>

最佳答案

您的 getAllInstituteNameList 需要模型集合(列表)。 您的 getName 返回单个模型(与上述预期类型冲突的内容)。 您的 getName 尝试返回 Collection 但预期结果是单个模型。又一个错误。

[NonAction]
private List<Model>getName()
{
    var names = (from m in db.Models
                select m).ToList();
    return names;
}

[NonAction]
public List<Model> getAllInstituteNameList()
{
    return getNames();
}

编辑

将模型返回到 View

return View(model);

关于c# - 无法将类型 'System.Linq.IQueryable<>' 隐式转换为 'Model' 。存在显式转换(是否缺少强制转换?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23403959/

相关文章:

c# - 使用 C# 在 ASP.NET MVC3 中进行模型绑定(bind)的好资源?

c# - 排序通用链表

c# - 为什么 AutoMapper 创建浅拷贝?

javascript - jQuery MaskMoney 插件和字段类型 Decimal(SQL SERVER 表)出现问题

javascript - 在 ASP.Net MVC 中使用成员资格会引发异常

c# - 在 MVC 4 脚本部分初始化 jQuery DataTable 时隐藏表数据的最佳方法是什么

c# - WebSecurity.IsAuthenticated 如何检查您是否已登录?

c# - 使用 Setter 更新样式触发器中的自定义附加属性

c# - 为什么我们不能像使用 WCF 或 ASMX 一样在 Visual Studio 中将 Web API 添加为 "service reference"?

C# ASP.NET MVC PayPal 找不到程序集