asp.net mvc login form in layout page (The partial view 'LogIn' was not found)

18,108

Solution 1

for returning a partial view you need to specify the partial view name. on your view you have the partial as _LogIn but on your controller you leave it as the default so it is looking for Login. Try changing your controller to

return PartialView("_LogIn", model);

Solution 2

u must name your partial view as ur method

@Html.Partial(_login)

and at the Controller

public ActionResult _login(...)
{
....
return PartialView(model);
}

or you can name them differently but ur Controller will be like that

public ActionResult Login(...)
{
... 
return PartialView("_login",model);
}

Solution 3

Because my _Layout page just has a model and I want to redirect to same page if form requires validation, so I used this line in my Controller:

return RedirectToAction("Index", "Home");

instead of returning PartialView as return PartialView("_LogIn", model);

thanks to all.

Share:
18,108
Azarsa
Author by

Azarsa

I am working Globaly with Corporations, Startups and Agencies. I design & develop compelling apps and engaging brands. All helping to improve response, boost profitability and drive sales. I am integrating into my clients’ teams, becoming their contractor of choice. I’ll fit seamlessly into your workflow. Whether that’s taking client feedback directly, or attending client meetings as ‘one of your agency team’, we never disclose who I am or what I do for your business.

Updated on June 14, 2022

Comments

  • Azarsa
    Azarsa almost 2 years

    In Asp.net MVC I have a login form on top of all pages (in _Layout page) and I put my login form in a PartialView as _Login in Shared Folder like this:

    @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>
    

    and in my _Layout page:

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

    and AccountController contains:

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

    When ModelState is not valid I get this error in browser:

    The partial view 'LogIn' was not found or no view engine supports the searched locations

    What is the problem with Controller? How can I return just _Login partialview in _Layout page on this line? :

    return PartialView(model);