ユーザーと小売業者のアカウントがあるアプリケーションがあります。小売業者の登録フォームの検証を実装したいと思います。私は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 ....
}
「間違った入力」と言うと、ユーザーが空白のままにしたフィールドを意味しますか? –
ビューコードを表示します。 –
はい@Harvey ..フィールドが空白のままになった –