簡體   English   中英

在MVC4 asp.net中包含路徑表達式錯誤

[英]Include path expression error in MVC4 asp.net

所以我正在嘗試進行自定義登錄。 我有一個登錄頁面,我想檢查用戶名和密碼是否與我的sql db匹配。 嘗試在文本框中輸入用戶名並將其與數據庫中的用戶名匹配時,我遇到了一個問題。 我收到一條錯誤,指出Include路徑表達式必須引用該類型上定義的導航屬性。 使用虛線路徑作為參考導航屬性,使用Select運算符作為集合導航屬性。 這是什么意思?

這是我的用戶控制器:

[HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(Models.User user)
        {
            if (ModelState.IsValid)
            {
                if (IsValid(user.UserName, user.Password))
                {
                    FormsAuthentication.SetAuthCookie(user.UserName, false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", "Login Data is incorrect");
                }
            }

            return View(user);
        }

        private bool IsValid(string UserName, string Password)
        {
            bool isValid = false;

                var user = db.User.Include(u => u.UserName == UserName);

                if (user != null)
                {
                    isValid = true;
                }

                return isValid;
        }

該錯誤在var user = db.User.Include(u => u.UserName == UserName);

這是我的cshtml:

@model MyApp.Models.User

@{
    ViewBag.Title = "Login";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true, "Login Failed.  Check your login details.");
    <div>
        <fieldset>
            <legend>Login Form</legend>
            <div>@Html.LabelFor(u=>u.UserName)</div>
            <div>@Html.TextBoxFor(u => u.UserName)
                @Html.ValidationMessageFor(u => u.UserName)
            </div>

            <div>@Html.LabelFor(u => u.Password)</div>
            <div>
                @Html.PasswordFor(u => u.Password)
            @Html.ValidationMessageFor(u => u.Password)
        </div>
            <input type="Submit" value="Log In" />
        </fieldset>
    </div>
}

這是用戶模型:

public class User
    {
        [Key]
        public int UserId { get; set; }

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

        public string FirstName { get; set; }
        public string LastName { get; set; }

        [Required]
        [StringLength(20, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }
    }
}

您不需要在此處使用Include,因為Include方法是獲取導航屬性。 如果您的目的是確定現有用戶,則使用“.Where”或“.Find”。

編輯:“.Find”方法需要使用主鍵來查詢dbcontext。 如果在上下文中找到對象,則不會往返數據庫。 使用“。Where”將匹配您傳遞給方法的任何謂詞。

您的查詢不正確。 .Include()方法用於檢索模型上的導航屬性。 您收到的錯誤消息表明了這一點,盡管有點令人困惑。 要檢索單個用戶,請使用:

var user = db.user.SingleOrDefault(u => u.UserName == UserName);

在這種情況下,如果數據庫中不存在具有指定用戶名的用戶,則user將為null。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM