0

問題:MVCブートストラップモーダルショーの検証結果

私はモーダルにエラーメッセージを取得するために、検証faildモーダルにしている場合で形成するために提出するプリフォームしたい

私は、エラーメッセージを表示するために、モーダルで提出したがfaildステーに実行するためのエレガントな方法はありhere

など詳細なAJAXの検証(jQueryの)を使用していますか?

マイコード:

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create(Cam c) 
    { 
     ViewBag.id = c.id; 

     using (Entities db = new Entities()) 
     { 
      if (ModelState.IsValid) 
      { 
       db.camp.Add(c); 
       db.SaveChanges(); 
       return RedirectToAction("Index", new { id = c.id }); 
      } 

     } 
     return null; 
    } 

クライアント:

@using (Html.BeginForm("Create", "Camp", FormMethod.Post, new { model = 
Model })) 
{ 
@Html.AntiForgeryToken() 

<dt> 
      name: 
     </dt> 
     <dd> 
      @Html.TextBoxFor(model => model.name, new { @class = "form-control", @placeholder = "name", @id = "txtVenueID", style = "width:150px" }) 
     </dd> 

     <dd> 
      @Html.ValidationMessageFor(model => model.name) 
     </dd> 
<div class="modal-footer "> 
<button type="button" class="btn btn-default" data- 
dismiss="modal">Close</button> 
<button type="submit" class="btn btn-primary">Save</button> 
</div> 
} 

モデル:

public partial class Cam 
{ 

    [Display(Name = "Name")] 
    [Required(ErrorMessage = "Require {0}")] 
    string name { get; set; } 
} 
+0

; '。しかし、[ValidateAjax]とは何ですか?あなたはモーダルを提出するためにアヤックスコールをしていますか? –

+0

@StephenMueckeあなたは正しく私は気づいていない、私はあなたの質問を残して提出したので私はサーバーの検証とモーダル滞在が妥当性検査の結果に依存していることを言い換えられました – shdr

+0

あなたは正しくクライアント側の検証を実装している場合、フォームは無効です。しかし、クライアント側の検証をバイパスすることができるので、 'ModelState'が無効であればビューを返す必要があります。しかし、モーダルなので、返されたときにそのモーダルを開くには少しのjavascriptが必要です。 –

答えて

0
  1. はそれを構築するが、モデル状態エラーを保つ作用を介して、モーダルで最初のフォームを表示する - フォームを送信しますが、モーダル使用で次のように結果を維持するためにビューについてThis
  2. を使用します。

    <button type="submit" class="btn btn-primary">Save</button> 
    
  3. Javascriptをモーダルに提出する結果を得るために: `リターンビュー(C)とnull`なので返す`置き換える

    $(function() { 
        $.ajaxSetup({ cache: false }); 
    
        $(':submit[data-modal]').on("click", function (e) { 
        e.preventDefault(); 
        var linkObj = $(this).closest('form'); 
         $.ajax({ // create an AJAX call... 
          data: linkObj.serialize(), // get the form data 
          type: linkObj.attr('method'), // GET or POST 
          url: linkObj.attr('action'), // the file to call 
          success: function(response) { // on success.. 
           $('#Modal-Content').html(response); // update the DIV 
          } 
         }); 
        }); 
    }); 
    
関連する問題