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

StackOverflow https://stackoverflow.com/questions/23403959

Question

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))%>

  • edited

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>
<% }%>
Was it helpful?

Solution

Your getAllInstituteNameList expects collection of models (List). Your getName returns single Model (what conflicts with above expected type). Your getName tries to return Collection but expected result is a single Model. One more error.

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

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

EDIT

return model to the view

return View(model);

OTHER TIPS

As you see in your code returning type of method is Model and you return a Linq object that's why it through exception.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top