2012-03-14 9 views
2

ユーザーと小売業者のアカウントがあるアプリケーションがあります。小売業者の登録フォームの検証を実装したいと思います。私はRequired Connotationを使って小売業者向けのモデルを作ったが、間違った入力のメッセージは表示されない。どのように私は私の目標を達成することができる任意の提案。 Asp.net MVC必須属性のモデルでは、不適切なユーザー入力のエラーメッセージは表示されません。

[PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "The password and confirmation password do not match.")] 
public class RegisterStore 
{ 
    [Required(AllowEmptyStrings = false, ErrorMessage = "Store Name is Required")] 
    [DataType(DataType.Text)] 
    [Display(Name = "Store Name")] 
    public string Store_Name { get; set; } 
    . 
    .  
    Similarly other properties ... 
    . 
    . 

} 

Viewのコードを使用してイムコントローラのコードは

[HttpPost()] 
    public ActionResult RetailerRegisteration(RegisterStore storeModel) 
    { 
     //ViewData["genders"] = Genders; 
     Debug.WriteLine("Started RetailerRegisteration"); 
     if (string.IsNullOrEmpty(storeModel.UserName)) 
      ModelState.AddModelError(string.Empty, "Please enter Username");  
     if (string.IsNullOrEmpty(storeModel.Store_Name)) 
      ModelState.AddModelError(string.Empty, "Please enter a store name"); 
     if (!string.IsNullOrEmpty(storeModel.Email) || !storeModel.Email.Contains("@")) 
      ModelState.AddModelError(string.Empty, "Please enter a valid e-mail address!"); 
     if (string.IsNullOrEmpty(storeModel.Password)) 
      ModelState.AddModelError(string.Empty, "Please enter a Password"); 
     if(! storeModel.Password.Equals(storeModel.ConfirmPassword)) 
      ModelState.AddModelError(string.Empty, "The Passwords must match"); 

     if (ModelState.IsValid) 
     { 
      ... Create Store Account .... 

     } 
+0

「間違った入力」と言うと、ユーザーが空白のままにしたフィールドを意味しますか? –

+1

ビューコードを表示します。 –

+0

はい@Harvey ..フィールドが空白のままになった –

答えて

2

のようなものです

<h2>Create a Store Account</h2> 
<p> 
    Use the form below to create a new account. 
</p> 
<% using (Html.BeginForm()) 
    {%> 
    <%: Html.ValidationSummary(true) %> 

    <fieldset> 
     <legend>Retailer Information</legend> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.Store_Name) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(model => model.Store_Name) %> 
      <%: Html.ValidationMessageFor(model => model.Store_Name) %> 
     </div> 
     . 

     ... Similar DIVs for other properties .... 
     . 
     . 
     . 
     . 

     <p> 
      <input type="submit" value="Next" /> 
     </p> 
    </fieldset> 

<% } %> 

のようであるすべてのエラーを表示するように<%: Html.ValidationSummary(false) %>を試してみてください。

また、thisの質問を参照してください。

+0

あなたはそれを本当に設定することを意味しますか? – WaZ

2

クライアント検証またはサーバー側検証のみを使用していますか?

サーバー側を確認している場合は、POSTでModelState.IsValidを確認していますか?このプロパティをチェックするまで、検証は実際には起動しません。

0

MVC2またはMVC3を使用していますか?両者には微妙な違いがあります。いずれの場合でも、適切なjQueryまたはAjax検証JavaScriptスクリプトを組み込んで、クライアント側の検証を取得する必要があります。

関連する問題