如何将ObjectResult转换为存储过程的List对象在MVC中?

时间:2016-02-03 11:54:18

标签: c# asp.net-mvc model-view-controller

我是MVC的新手,正在进行登录页面验证,并且在控制器中遇到问题,通过带有.edmx文件的存储过程获取登录用户数据。

PROC_LogIn_Info是存储过程的名称。

我收到错误:

  

无法将方法组'ToList'转换为非委托类型'System.Collections.Generic.List'。你打算调用这个方法吗?

代码:

[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");
}

我的模型是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; }
}

4 个答案:

答案 0 :(得分:1)

首先在你的cs文件中做一个引用,然后使用System.Linq中的ToList:

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;  
    }

答案 1 :(得分:0)

存储过程的结果是一种唯一的自动生成类型。最简单的转换方法是

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

答案 2 :(得分:0)

使用复杂类型的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");
    }

答案 3 :(得分:0)

我搜索了很多文章,但没有找到正确的答案。因此,我创建了示例代码。希望这会有所帮助。

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();
        }

在上面的代码中,dbContext是edmx上下文引用。 UserEntities()是我的edmx实例的名称。 GetUserDetails()是存储过程的名称,而User是模型类。希望能帮助您:)

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