I am new in MVC, I dont know much about MVC, so sometimes i cant got the error,
I am creating dual list box and move the items of listbox1 to listbox2, in first listbox fetch the data from database, so for that i have create the following..
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; }
}
in controller to fetch the data from db i have create the following
[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();
}
After runnig, it throws the following exception
Cannot implicitly convert type
'System.Linq.IQueryable' to
'DemoListBox.Models.Model'. An explicit conversion exists (are you
missing a cast?)
on this line
var name = (from m in db.Models
select m);
return name;
Please help to solve it.....
UPDATED
After following your ans..
i again got this new exception on view
Object reference not set to an instance of an object.
in this line
<%:Html.ListBoxFor(model =>model.AvailableSelected, newMultiSelectList(Model.AvailableModel, "Id", "Name", Model.AvailableSelected))%>
View
<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>
<% }%>