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);
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);
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);
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.