【问题标题】:Cannot implicitly convert type 'System.Linq.IQueryable<>' to 'Model'.An explicit conversion exists (are you missing a cast?)无法将类型“System.Linq.IQueryable<>”隐式转换为“Model”。存在显式转换(您是否缺少演员表?)
【发布时间】:2014-05-01 07:46:52
【问题描述】:

我是 MVC 的新手,我对 MVC 了解不多,所以有时我无法得到错误, 我正在创建双列表框并将 listbox1 的项目移动到 listbox2,在第一个列表框中从数据库中获取数据,因此我创建了以下内容。 Model.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; }
}

在控制器中从数据库中获取数据我已经创建了以下

[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();
}

runnig之后,抛出如下异常

不能隐式转换类型 'System.Linq.IQueryable' 到 'DemoListBox.Models.Model'。存在显式转换(您是 缺少演员表?)

在这条线上

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

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

更新

在关注你的回答之后.. 我再次看到这个新异常

对象引用未设置为对象的实例。

在这一行

<%: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>
<% }%>

【问题讨论】:

  • 能否也粘贴您的View 的代码
  • @lnanikian 查看已编辑

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


【解决方案1】:

您的 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();
}

编辑

将模型返回视图

return View(model);

【讨论】:

  • 问:写完这个私有模型 getName() { var name = (db.Models).First();返回名称;它再次抛出此异常无法将类型'DemoListBox.Models.Model'隐式转换为'System.Collection.Generic.List'
  • @user3449277 请编辑您的问题,而不是答案
  • @user3449277 在返回模型以查看之前初始化模型中的所有属性
  • @user3449277 您没有返回查看。检查编辑
  • 哦...我忘记了..非常感谢您的帮助..Thankssss
【解决方案2】:

正如您在代码中看到的,返回方法的类型是模型,并且您返回一个 Linq 对象,这就是它通过异常的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多