0
votes

I am new in MVC. I am tryinng to load username from databse to dropdown list. but get error in my model class-

Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)

My Model class is-

DropdownClass.cs Here is my model class code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LoginForm.Models;

    namespace LoginForm.Models
    {
        public class DropdownClass
        {

            ConsumerBankingEntities db = new ConsumerBankingEntities();
            public IEnumerable<UserInfo_Result> GetUserInfo()
            {
                var query = (from s in db.UserInfo()
                            select s);
                return query.ToList();

            }
        } 
    }

My controller name is

LoginController.Cs

Here is the controller code-

using LoginForm.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Data.SqlClient;


namespace LoginForm.Controllers
{
    public class LoginController : Controller
    {

        ConsumerBankingEntities db = new ConsumerBankingEntities();


        public ActionResult Dropdown(string returnUrl)
        {

            DropdowncCass dp = new DropdowncCass();
            var allName = dp.GetUserInfo();
            ViewBag.AllNameList = allName;
            Session["allName"] = allName;
            ViewBag.ReturnUrl = returnUrl;

            return RedirectToAction("Welcome","Login");

        }


        // GET: Login
        public ActionResult Index()
        {
            return View();
        }


        public ActionResult Welcome()
        {
            return View();
        }

    }
}

How can get over this error?

1
Is this a compilation error? In which line it occurs. - SᴇM
Yes. It is. in my model class. red line appears under " return query.ToList();" @SeM - Ken Kaneki
Issue is you are returning a different type via linq query from UserInfo_Result, which is why there's an error - Mrinal Kamboj

1 Answers

0
votes

It's because your db.UserInfo() method returns collection of string and ToString() makes it List<string>, but method GetUserInfo() returns IEnumerable<UserInfo_Result>.

You should either change your db.UserInfo() to return IEnumerable<UserInfo_Result>, or construct IEnumerable<UserInfo_Result> before returning (at GetUserInfo), or just change return type of GetUserInfo to List<string>. It dependes on what you need.