布局页面中的asp.net mvc登录表单(未找到部分视图'LogIn')

Aza*_*rsa 2 c# asp.net asp.net-mvc asp.net-mvc-4

在Asp.net MVC中,我在所有页面上都有一个登录表单(在_Layout页面中),我将我的登录表单作为_Login放在PartialView中的共享文件夹中,如下所示:

@model MyProject.ViewModels.LogInModel
<div id="popover-head" class="hide">Login</div>
<div id="popover-content" class="hide">
    @using (Ajax.BeginForm("LogIn", "Account", new AjaxOptions { UpdateTargetId = "login" }))
    {
        @Html.TextBoxFor(m => m.UserName, new { @class = "input-block-level", placeholder = "username" })    
        @Html.ValidationMessageFor(m => m.UserName)    

        @Html.PasswordFor(m => m.Password, new { @class = "input-block-level", placeholder = "password" })
        @Html.ValidationMessageFor(m => m.Password)

        <input type="submit" name="login" value="login" class="btn btn-primary" />

        @Html.CheckBoxFor(m => m.RememberMe)
        @Html.LabelFor(m => m.RememberMe)
    }
</div>
Run Code Online (Sandbox Code Playgroud)

在我的_Layout页面中:

<a id="popover" href="#" class="btn" data-toggle="popover" data-placement="bottom">Login</a>    
 <div id="login">
    @Html.Partial("_LogIn")                     
 </div>
Run Code Online (Sandbox Code Playgroud)

和AccountController包含:

[HttpPost]
    public ActionResult LogIn(LogInModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (MembershipService.ValidateUser(model.UserName, model.Password))
            {

                FormsService.SignIn(model.UserName, model.RememberMe);
                if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                {
                    return Redirect(returnUrl);
                }                    

                return RedirectToAction("Index", "Home");
            }
            ModelState.AddModelError("", "login failed");
        }

        return PartialView(model);
    }
Run Code Online (Sandbox Code Playgroud)

当ModelState无效时,我在浏览器中收到此错误:

未找到部分视图"LogIn"或视图引擎不支持搜索的位置

Controller有什么问题?如何在此行的_Layout页面中返回_Login partialview?:

return PartialView(model);
Run Code Online (Sandbox Code Playgroud)

Mat*_*ily 5

要返回部分视图,您需要指定部分视图名称.在你的视图中你有部分为_LogIn,但在你的控制器上,你把它作为默认值,所以它正在寻找登录.尝试将控制器更改为

return PartialView("_LogIn", model);
Run Code Online (Sandbox Code Playgroud)