0

mvc4アプリでログインと登録タスクを管理するための単一のページを作成します。登録を使用してユーザーを作成し、登録が失敗した場合(パスワードの不一致など)、ページはエラーを処理して更新しますが、登録のrenderaction部分に完全なページを挿入します。

一方、ログインに失敗した場合(ユーザーが存在しない場合)、ページは更新されますが、レイアウト全体ではなく、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);
}
4

1 に答える 1

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の両方を含むAccessクラスをビューのモデルとして使用する必要があります。@modelアクセス

また、ログインモデルまたは登録モデルをレンダリングするときは、モデルパラメータを渡す必要があります。これにより、ユーザーはデータを再入力する必要がなくなります。*編集-また、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);
}

ああ、また、ビューバッグを使用して最初にデータを渡す代わりに、モデルを使用してデータを渡す必要があります-かみそりビューでコード「@ModelAcesss」を使用することを選択したため、ActionResultを登録します次のようになります...

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

あなたがMVCにそれほど慣れていないことに気づきませんでした。それを維持します。:)別のチュートリアルが役立つかもしれません

于 2013-03-24T00:42:34.320 に答える