简体   繁体   中英

How to convert ObjectResult into List object for stored procedure In MVC?

I am new to MVC and working on Login Page validation, and having problem in controller for fetching login user data through a stored procedure with an .edmx file.

PROC_LogIn_Info is the name of the stored procedure.

I get an error :

Cannot convert method group 'ToList' to non-delegate type 'System.Collections.Generic.List'. Did you intend to invoke the method?

Code:

[HttpPost]
public ActionResult Login(tblUserMaintenance user)
{
        if (ModelState.IsValid)
        {
            using (dbPA_MVCEntities objCon = new dbPA_MVCEntities())
            {                   
                List<Login> LoginUser =  objCon.PROC_LogIn_Info("jhony", "a").ToList();
                // showing Error here

            }

        }
        else
            ModelState.AddModelError("", "The user name or password provided is incorrect.");


        return RedirectToAction("LoginIndex", "Login");
}

My model is Login :

public class Login
{
    [Required]
    [DisplayName("User ID")]
    public string vUserID { get; set; }

    [DisplayName("User Name")]
    public string vUserName { get; set; }

    [DisplayName("User Email Id")]
    public string vUserEmail { get; set; }

    [DisplayName("Phone Number")]
    public string vPhoneNumber { get; set; }

    public string vRoleId { get; set; }
    public string IsActive { get; set; }
    public string chUserType { get; set; }

    [Required]

    [DisplayName("Password")]
    public string vPassword { get; set; }

    public List<Login> LoginUsersData { get; set; }
}

First of all make a reference to System.Linq in your cs file, then use ToList from System.Linq :

using System;
using System.Collections.Generic;
using System.Linq;


public IList<ActivityEvent> GetActivityEvent(string sESSION_ID)
    {
        var result = this.unitOfWork.Context.GetActivityEvent(sESSION_ID);
        if (result != null)
        {
            return result.ToList();
        }

        return null;  
    }

I Searched many Articles but didn't see a proper answer for this. So, i have created a sample piece of code. Hope this helps.

static void Main(string[] args)
{
List<User> myUserList = GetUserList();
}
public static List<User> GetUserList()
        {
            var dbContext = new UserEntities();
            var results = dbContext.GetUserDetails();
            return results.Select(x => new User
            {
                User_id = x.User_id,
                First_Name = x.First_Name,
                Last_Name = x.Last_NAME
            }).ToList();
        }

In the above piece of code, dbContext is the edmx context reference. UserEntities() is the name of my edmx instance. GetUserDetails() is the name of stored procedure and User is the model class. HOPE THIS HELPS :)

References: https://forums.asp.net/t/2072373.aspx?Can+not+implicitly+convert+List+objectresult+T+to+collection+GenericList+T+

The result from a stored procedure is a unique, autogenerated type. The simplest way to do the conversion is

List<Login> LoginUser = from result in objCon.PROC_LogIn_Info("jhony", "a").ToList()
                        select new Login
                        {
                            // Set each property in turn
                            vUserID = result.UserId;
                            ...
                        };

by using complex type of PROC_LogIn_Info_Result

 [HttpPost]
    public ActionResult Login(Login user)
    {
        if (ModelState.IsValid)
        {
            using (dbPA_MVCEntities objCon = new dbPA_MVCEntities())
            {                    
                List<PROC_LogIn_Info_Result> LoginUser = objCon.PROC_LogIn_Info(user.vUserid, user.vPassword).ToList<PROC_LogIn_Info_Result>();
                if (LoginUser.Count > 0)
                {
                    Session["UserID"] = LoginUser[0].vUserid;
                    Session["UserName"] = LoginUser[0].vUserName;
                    Session["UserRole"] = LoginUser[0].vRole;
                    Session["UserType"] = LoginUser[0].vUserType;
                    Session["UserEmailId"] = LoginUser[0].vUserEmail;
                }
                else
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");                       
            }

        }
        else
            ModelState.AddModelError("", "The user name or password provided is incorrect.");


        return RedirectToAction("LoginIndex", "Login");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM