2016-04-06 39 views
0

MVCの学習を始めたばかりで、コントローラーで複数のアクションを使用するのか、単純な登録ページを作成するのかという疑問がありました。ASP.NET MVC複数のアクションを1つのジョブで処理する

が、私はこの(複数のアクション)のような何かを行う必要があります。

HTML(RegisterForm)

<form action="CheckRegistration" method="post"> 
     Username: 
     <input type="text" name="username"/> 
     <input type="submit" value="Login" /> 
    </form> 

コントローラ

public ActionResult RegisterForm() 
    { 
     return View(); 
    } 
    public ActionResult CheckRegistration() 
    { 
     bool success = true; 

     // Create User Object and populate it with form data 
     User currentUser = new User(); 
     currentUser.Username = Request.Form["username"].Trim().ToString(); 

     // Validate Registration 
     // code 

     // Add user to database 
     // code 

     if (success) 
     { 
      return View("Login"); 
     }else 
     { 
      return View("RegistrationSuccess"); 
     } 
    } 

またはこの(シングルアクション):

HTML (登録)

<form action="Register" method="post"> 
     Username: 
     <input type="text" name="username"/> 
     <input type="submit" value="Login" /> 
    </form> 

コントローラ

public ActionResult Register() 
    { 
     bool success = true; 
     String otherData = "" 

     // Create User Object and populate it with form data 
     User currentUser = new User(); 
     currentUser.Username = Request.Form["username"].Trim().ToString(); 

     // Validate Registration 
     // code 

     // Add user to database 
     // code 

     if (success) 
     { 
      return View("Login"); 
     }else 
     { 
      return View("Register", otherData); 
     } 
    } 

私が考える最初の方法では、それは複数のアクションを持っており、複数のステップに分離します。

2つ目の方法では1つのアクションしか使用しないため、レジスタビューを最初に呼び出すと、検証に失敗してView()を返すだけなので、ユーザーはデータベースに追加されません。

いずれにしても、プロフェッショナルの立場からはどちらが良いか(どちらが良いか)、どちらも悪い方法であり、より良い方法があります。

+0

としての第2のメソッドを宣言? –

+0

'' HttpGet''メソッドと '' HttpPost''メソッドが必要ですが、 '' [HttpGet] public ActionResult Register() ''と '' HttpPost''のpublic ActionResult Register(RegisterModelモデル)と同じ名前にすることができます。 –

+1

しかし、MVCサイトに行ってMVCの基本を学ぶ必要があります。具体的には、モデルを使用して表示する方法、そしてHtmlHelperメソッドを使用してモデルに強くバインドする必要があります。 –

答えて

1

あなたは簡単な表示をして、投稿にログインする必要があります。

// GET: /Account/Register 
    [AllowAnonymous] 
    public ActionResult Register() 
    { 
     return View(); 
    } 

    // 
    // POST: /Account/Register 
    [HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Register(RegisterViewModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      var user = new ApplicationUser() { UserName = model.Email, Email = model.Email }; 
      IdentityResult result = await UserManager.CreateAsync(user, model.Password); 
      if (result.Succeeded) 
      { 
       await SignInAsync(user, isPersistent: false); 

       // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 
       // Send an email with this link 
       // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); 
       // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); 
       // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); 

       return RedirectToAction("Index", "Home"); 
      } 
      else 
      { 
       AddErrors(result); 
      } 
     } 

     // If we got this far, something failed, redisplay form 
     return View(model); 
    } 
0

MVCで同じ名前のメソッドを使用できます。

1)は、そう、それはビューと

2を返します[HttpGet]としてまずメソッドを宣言します)1話の失敗一挙にユーザーを登録することを防ぐだろうか[HttpPost]

[HttpGet] 
public ActionResult RegisterForm() 
{ 
    return View(); 
} 

[HttpPost] 
public ActionResult RegisterForm() 
{ 
    bool success = true; 
    String otherData = "" 

    // Create User Object and populate it with form data 
    User currentUser = new User(); 
    currentUser.Username = Request.Form["username"].Trim().ToString(); 

    // Validate Registration 
    // code 

    // Add user to database 
    // code 

    if (success) 
    { 
     return RedirectToAction("RegisterSuccess"); 
    }else 
    { 
     return View("RegisterForm"); 
    } 
} 
関連する問題