2017-07-13 12 views
0
私は、私はこれを実行するために、任意のフロントエンドの言語を使用していないフォームからいくつかのフィールドを検証する必要が

とポップアップメッセージを表示する方法、私はActionResultから情報を得ている私はいくつかを示す必要がありますメッセージ!ここでASPNETレイザー

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult ResetPassword(string user, string hash) 
    { 
     string old = Request.Form["oldpassword"]; 
     string password = Request.Form["password"]; 
     string repeat = Request.Form["repeatpassword"]; 


     if(String.IsNullOrEmpty(old) || String.IsNullOrEmpty(password) || String.IsNullOrEmpty(repeat)) 
     { 
      "show a dialog". 
     } 
+0

あなたはJSモーダルポップアップにメッセージ値を渡すことができます:https://stackoverflow.com/questions/13183630/how-to-open-a-bootstrap-modal-window-using-jquery。 viewmodelに基づいてモーダルポップアップテキストを設定するだけです。 '$("。モーダルボディ ")。テキスト( '@ Model.Message')'。 –

+1

あなたはそれがあなたの '@ Html.ValidationSummary()'プレースホルダにsisplayefされるように、 'ModelStateError'を追加し、ビューを返し、その後、通常のPOSTを行った場合。 –

+0

'ModelState.AddModelError'方法も有効である、少なくとも適切な場所に' 'ValidationSummary'またはValidationMessage'を使用します。' $( "モーダル・ボディ")、テキスト( '@ Html.ValidationSummary()') '。 –

答えて

1

はそれを行うには良い方法です:

ビュー:

@model Testy20161006.Controllers.ResetPasswordViewModel 
@{ 
    Layout = null; 
} 

<!DOCTYPE html> 

<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>IndexPage2</title> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css"> 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> 
</head> 
<body> 
    @{ 
     if (ViewBag.ShowDialog != null && ViewBag.ShowDialog == true) 
     { 
      <script type="text/javascript"> 
       $(function() { 
        $("#aModal").modal('show'); 
       }) 
      </script> 
     } 
    } 
    @using (Html.BeginForm("ResetPassword", "Home")) 
    { 
     <div> 
      <table> 
       <tr> 
        <td>@Html.LabelFor(r => r.oldPassword)</td> 
        <td>@Html.TextBoxFor(r => r.oldPassword)</td> 
       </tr> 
       <tr> 
        <td>@Html.LabelFor(r => r.newPassword)</td> 
        <td>@Html.TextBoxFor(r => r.newPassword)</td> 
       </tr> 
       <tr> 
        <td>@Html.LabelFor(r => r.repeatNewPassword)</td> 
        <td>@Html.TextBoxFor(r => r.repeatNewPassword)</td> 
       </tr> 
      </table> 
     </div> 
     <input type="submit" value="submit" /> 
    } 
    @*modal*@ 
    <div class="modal fade" id="aModal" tabindex="-1" role="dialog" aria-labelledby="aModalLabel" 
     aria-hidden="true"> 
     <div class="modal-dialog" role="document"> 
      <div class="modal-content"> 
       <div class="modal-header"> 
        <h4 class="modal-title" id="aModalLabel">Warning</h4> 
       </div> 
       <div class="modal-body"> 
        Old Password, New Password, or repeat Password is empty. 
       </div> 
       <div class="modal-footer"> 
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> 
       </div> 
      </div> 
     </div> 
    </div> 
</body> 
</html> 

コントローラ/ビューモデル:

public class ResetPasswordViewModel 
{ 
    public string oldPassword { get; set; } 
    public string newPassword { get; set; } 
    public string repeatNewPassword { get; set; } 
} 

public class HomeController : Controller 
{ 
    [HttpPost] 
    public ActionResult ResetPassword(ResetPasswordViewModel resetPasswordViewModel) 
    { 
     if (String.IsNullOrEmpty(resetPasswordViewModel.oldPassword) || 
      String.IsNullOrEmpty(resetPasswordViewModel.newPassword) || 
      String.IsNullOrEmpty(resetPasswordViewModel.repeatNewPassword) 
      ) 
     { 
      //show dialog 
      ViewBag.ShowDialog = true; 
     } 

     return View("IndexPage2", resetPasswordViewModel); 
    } 

    public ActionResult IndexPage2() 
    { //start page specified in routeconfig.cs 
     return View(); 
    } 
+0

はのは、私はより多くのいくつかのことを検証する必要があるとしましょうか? 'ViewBag.ShowDialog = true;'はcshtml内のロジックをどのように見分けるのでしょうか? –

+1

多くのビューバッグが機能します。最善の方法は、[必須]属性をビューモデルの必須プロパティに配置することです。アクションで、ModelState.Validをチェックし、dbまたはresetパスワードへの書き込みが有効である場合は、ビューを再度表示します。ビューは自動的にどのフィールドがバリデーションを満たさないかを示します。 Html.ValidationSummary(false)はすべてのエラーを表示し、Html.ValidationMessageFor(model => model.FieldName)はフィールドによってエラーを表示します。 – kblau

+0

ありがとう! –