部分失败时阻止整页重新加载 - 登录注册

时间:2013-03-23 23:25:21

标签: c# asp.net-mvc-4 simplemembership

我创建了一个单独的页面来管理我的mvc 4应用程序中的登录和注册任务。当我使用注册创建用户,并且注册失败 - 例如密码不匹配 - 页面处理错误并刷新但在寄存器的renderaction部分插入一个完整的页面。

另一方面,当登录失败时 - 用户不存在 - 页面刷新但是将用户引导到仅显示渲染登录部分的页面,而不是整个布局..任何帮助表示赞赏:

步骤:我创建了一个模型

public class Access
{
    public LoginModel LoginModel { get; set; }
    public RegisterModel RegisterModel { get; set; }
}

然后我创建了一个页面:

<div class="main-content" style="min-height: 700px;">
    <!-- Login form -->
    @{ Html.RenderAction("Login", "Account"); }

    <!-- Register form -->
    @{ Html.RenderAction("Register", "Account"); }
    <div class="clear"></div>
</div>

控制器是mvc4模板中提供的典型控制器。登录的一个例子

public ActionResult Login(LoginModel model, string returnUrl)
{
    if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
    {
        if (Url.IsLocalUrl(returnUrl))
        {
            return Redirect(returnUrl);
        }
        else
        {
            return RedirectToAction("Index", "Home");
        }
    }

    // If we got this far, something failed, redisplay form
    ModelState.AddModelError("", "The user name or password provided is incorrect.");
    return PartialView(model);
}

1 个答案:

答案 0 :(得分:1)

您在失败时返回ParitalView,您可能想要返回包含整个页面的视图。即 修改

//recreate the compound viewmodel in the login controller
 Access viewModel = new Access();
 viewModel.LoginModel = model;
 viewModel.RegisterModel = new RegisterModel();
return View("LoginOrRegister", viewModel); //supposing the view name is LoginOrRegister.cshtml

//recreate the compound viewmodel in the register controller
 Access viewModel = new Access();
 viewModel.LoginModel = new LoginModel();
 viewModel.RegisterModel = model;
 return View("LoginOrRegister", viewModel); //supposing the view name is LoginOrRegister.cshtml

loginorregister.cshtml页面也必须使用包含LoginModel和RegisterModel作为View模型的Access类,即。    @model Access

当您渲染登录或注册模型时,您需要传入模型参数 - 这样用户就不需要重新输入数据。 *编辑 - 也尝试从RenderAction更改为Html.RenderPartial(),以便它不会通过初始渲染的操作进行路由 - 执行此操作并在控制器上添加[HttpPost]注释*代码看起来像

@model Access
<div class="main-content" style="min-height: 700px;">
<!-- Login form -->
@{ Html.RenderPartial("Login", new {model = Model.LoginModel, returnUrl = "?"}) };

<!-- Register form -->
@{ Html.RenderPartial("Login", "Account", new { model = Model.RegisterModel) }; }
<div class="clear"></div>
</div>

[HttpPost]
public ActionResult Login(LoginModel model, string returnUrl) 
{
    if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie:     model.RememberMe))
    {
    if (Url.IsLocalUrl(returnUrl))
    {
        return Redirect(returnUrl);
    }
    else
    {
        return RedirectToAction("Index", "Home");
    }
}

// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");

//recreate the compound viewmodel in the login controller
Access viewModel = new Access();
viewModel.LoginModel = model;
viewModel.RegisterModel = new RegisterModel();
return View("LoginOrRegister", viewModel);
}

啊,也不是使用viewbag最初传入数据,而是必须使用模型传递它 - 因为我选择在剃刀视图中使用代码“@Model Acesss”,所以注册表ActionResult看起来像......

public ActionResult Register(){
    Access viewModel = new Access();
    viewModel.LoginModel = new LoginModel();
    viewModel.RegisterModel = new RegisterModel();
    return View("LoginOrRegister",viewModel);
} 

我没有意识到你对mvc这么新。坚持下去。 :)另一个教程可能有帮助

相关问题