Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 传递到字典的模型项的类型为“System.Collections.Generic.List`1[Something.Models.Logins]”_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc 传递到字典的模型项的类型为“System.Collections.Generic.List`1[Something.Models.Logins]”

Asp.net mvc 传递到字典的模型项的类型为“System.Collections.Generic.List`1[Something.Models.Logins]”,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我的ASP.NET MVC4项目有问题 我为我的登录系统创建了一个创建页面/注册页面。但当我创建帐户时,它会给我以下错误: 传递到字典的模型项的类型为“System.Collections.Generic.List`1[Something.Models.Logins]”,但此字典需要类型为“Something.Models.Logins”的模型项 我在Stackoverflow上搜索了一会儿,发现了一些修复程序,但它们对我的应用程序不起作用 这是我认为需要帮助的代码 我的控制器: [HttpPo

我的ASP.NET MVC4项目有问题

我为我的登录系统创建了一个创建页面/注册页面。但当我创建帐户时,它会给我以下错误:

传递到字典的模型项的类型为“System.Collections.Generic.List`1[Something.Models.Logins]”,但此字典需要类型为“Something.Models.Logins”的模型项

我在Stackoverflow上搜索了一会儿,发现了一些修复程序,但它们对我的应用程序不起作用

这是我认为需要帮助的代码

我的控制器:

[HttpPost]
public ActionResult Login(Models.Logins user)
{
    if (ModelState.IsValid)
    {
        if (user.IsValid(user.Username, user.Password))
        {
            FormsAuthentication.SetAuthCookie(user.Username, user.RememberMe);
            return RedirectToAction("Index", "Home");
        }
        else
        {
            ModelState.AddModelError("", "Login gegevens kloppen niet!");
        }
    }
    return View(user);
}
型号:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace RTApplicatie.Models
{
    public class Logins
    {
        [Required]
        public int ID { get; set; }

        [Required]
        [Display(Name = "User name")]
        public string Username { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [Display(Name = "Remember on this computer")]
        public bool RememberMe { get; set; }

        [Required]
        public bool Rights { get; set; }
        /// <summary>
        /// Checks if user with given password exists in the database
        /// </summary>
        /// <param name="_username">User name</param>
        /// <param name="_password">User password</param>
        /// <returns>True if user exist and password is correct</returns>
        public bool IsValid(string _username, string _password)
        {
            using (var cn = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;"))
            {
                string _sql = @"SELECT [Username] FROM [dbo].[System_Users] " +
                       @"WHERE [Username] = @u AND [Password] = @p";
                var cmd = new SqlCommand(_sql, cn);
                cmd.Parameters
                    .Add(new SqlParameter("@u", SqlDbType.NVarChar))
                    .Value = _username;
                cmd.Parameters
                    .Add(new SqlParameter("@p", SqlDbType.NVarChar))
                    .Value = Helpers.SHA1.Encode(_password);
                cn.Open();
                var reader = cmd.ExecuteReader();
                if (reader.HasRows)
                {
                    reader.Dispose();
                    cmd.Dispose();
                    return true;
                }
                else
                {
                    reader.Dispose();
                    cmd.Dispose();
                    return false;
                }
            }
        }
    }

    public class RekenContext : DbContext
    {
        public DbSet<Logins> RTApplicatie { get; set; }
    }
}
我还在视图中提供了Razor代码的这一部分:

@model IEnumerable<Something.Models.Logins>
登录操作返回的是Something.Models.Logins而不是列表,因此需要更改视图。设置:

@model Something.Models.Logins

您的错误再清楚不过了:您的视图需要一个IEnumerable类型的Models.Logins,而您的操作只传递一个Models实例。Logins:

public ActionResult Login(Models.Logins user)
{

    ...

    return View(user);
}

您显示的代码与错误无关。在某个地方,您有一个带有@model Something.Models.Logins的视图,您可能在生成该视图的GET方法中向其传递列表?另一种方式是,该视图需要一个项目,并传递一个collectionOkay。我从视图中删除了IEnumerable,但它仍然给我相同的错误。