【发布时间】: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