2016-10-28 12 views
0

MVC 5検証を使用して、ユーザーがページのすべてを正しく入力しなかった場合、エラーメッセージを表示するのに特に問題があります。フォームのHTMLマークアップは以下の通りである:MVC 5の検証エラーメッセージを表示できません。

   @using (Html.BeginForm("RegisterDo", "User", FormMethod.Post)) 
       { 
        <div> 
         @Html.ValidationMessageFor(model => model.FirstName) 
         @Html.TextBoxFor(m => m.FirstName, new { placeholder = "First name", @class = "form-control", @type = "text" }) 



        </div> 
        <div> 
         @Html.ValidationMessageFor(model => model.LastName) 
         @Html.TextBoxFor(m => m.LastName, new { placeholder = "Last name", @class = "form-control", @type = "text" }) 


        </div> 
        <div> 
         @Html.ValidationMessageFor(model => model.Email) 
         @Html.TextBoxFor(m => m.Email, new { placeholder = "Email", @class = "form-control", @type = "email" }) 

        </div> 
        <div> 
         @Html.ValidationMessageFor(model => model.Password) 
         @Html.TextBoxFor(m => m.Password, new { placeholder = "Password", @class = "form-control", @type = "password" }) 

        </div> 
        <div> 
         @Html.ValidationMessageFor(model => model.PasswordConfirm) 
         @Html.TextBoxFor(m => m.PasswordConfirm, new { placeholder = "Confirm password", @class = "form-control", @type = "password" }) 


        </div> 
        <div> 
         @Html.ValidationMessageFor(model => model.SelectedCountryId) 
         @Html.DropDownListFor(model => model.SelectedCountryId, Model.Countries, "-- Please select a country --", new { @class = "select2_single form-control select2-hidden-accessible", @tabindex = "-1" }) 

        </div> 
        <div> 
         <input class="btn btn-default submit" type="submit" value="Register" /> 
        </div> 
       } 

私はビューがレンダリングされるときに、私が最初に記入する必要があるドロップダウンリストを持っていることに注意してください(つまり、私は国が順番にビュー上に一覧表示渡す必要があります適切にレンダリングする)。ここにある問題は、「登録」ボタンをクリックすると、自分のアクションのコードがAT ALLでトリガーされないということです。これらは私の2つです...

http://localhost:60617/user/register?FirstName=&LastName=&Email=&Password=&PasswordConfirm=&SelectedCountryId= 

とエラーメッセージがまったく表示されない...コードはまったくトリガされません

を:代わりに、私は単に次のように私のブラウザでURLを取得しますアクション:

[HttpPost] 
     [ActionName("Register")] 

     public ActionResult DoRegister(UserRegistrationViewModel model) 

     { 
      if (ModelState.IsValid) 
      { 
       var user = new Users(); 
       user.FirstName = model.FirstName; 
       user.LastName = model.LastName; 
       user.Email = model.Email; 
       user.PasswordSalt = Helpers.PasswordHelper.CreateSalt(40); 
       user.PasswordHash = Helpers.PasswordHelper.CreatePasswordHash(model.Password, user.PasswordSalt); 
       user.CountryId = Convert.ToInt32(model.SelectedCountryId); 
       user.Active = true; 
       Connection.ctx.Users.Add(user); 
       Connection.ctx.SaveChanges(); 
       var role = new UserRoles(); 
       role.RoleId = 2; 
       role.UserId = user.UserId; 
       role.Active = true; 
       user.UserRoles.Add(role); 
       Connection.ctx.SaveChanges(); 
       return RedirectToAction("Success"); 
      } 
      else 
      { 
       return View("Register", model); 
      } 
     } 
     public ActionResult Register() 
     { 
      var model = new UserRegistrationViewModel(); 
      var countries = Connection.ctx.Countries.OrderBy(x => x.CountryName).ToList(); 
      model.Countries = new SelectList(countries, "CountryId", "CountryName"); 
      return View(model); 
     } 

最初のアクションは、ビューをレンダリングするための責任であり、ユーザ登録...

私はここで間違って何をやっているときに、第2の1のロジックを担当しています。ここ:(

編集がViewModelにある...私はここに非常に何かを明らかに逃しんだけど、私はちょうどカントはそれを参照してください。

public class UserRegistrationViewModel 
    { 
     [Required(ErrorMessage = "First name is required!")] 
     public string FirstName { get; set; } 

     [Required(ErrorMessage = "Last name is required!")] 
     public string LastName { get; set; } 

     [Required(ErrorMessage = "Email name is required!")] 
     public string Email { get; set; } 

     [Required(ErrorMessage = "Password name is required!")] 
     public string Password { get; set; } 

     [Required(ErrorMessage = "Password confirmation name is required!")] 
     public string PasswordConfirm { get; set; } 
     [Required(ErrorMessage = "Country needs to be selected!")] 
     public int SelectedCountryId { get; set; } 


     public SelectList Countries { get; set; } 
} 
+0

を最初は 'post'メソッド名を変更する必要はなく、モデルを表示できますか? – SeM

+0

モデルはどこにありますか?...あなたがプロパティに置いている検証を表示します。 –

+0

@SeM私はモデルを含めました – User987

答えて

1

を追加するためにどちらのアクションメソッド ヒットビューで、アクションの名前を変更するには:

[HttpPost] 
[ActionName("Register")] 
public ActionResult RegisterDo(UserRegistrationViewModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     var user = new Users(); 
     user.FirstName = model.FirstName; 
     user.LastName = model.LastName; 
     user.Email = model.Email; 
     user.PasswordSalt = Helpers.PasswordHelper.CreateSalt(40); 
     user.PasswordHash = Helpers.PasswordHelper.CreatePasswordHash(model.Password, user.PasswordSalt); 
     user.CountryId = Convert.ToInt32(model.SelectedCountryId); 
     user.Active = true; 
     Connection.ctx.Users.Add(user); 
     Connection.ctx.SaveChanges(); 
     var role = new UserRoles(); 
     role.RoleId = 2; 
     role.UserId = user.UserId; 
     role.Active = true; 
     user.UserRoles.Add(role); 
     Connection.ctx.SaveChanges(); 
     return RedirectToAction("Success"); 
    } 
    else 
    { 
     return View(model); 
    } 
} 
+0

の上にActionName( "register")属性を追加する場合は、 "Html.Beginform(...)"部分の内容を変更する必要がありますか? – User987

+0

いいえ、私は.....そのトリガされていない場合は、 'Html.BeginForm()の内部を削除します – SeM

+0

しかし、私はちょうどHtml.BeginForm()を残してどのようなアクションがトリガーされるのか分かりますか? :D – User987

1
else 
{ 
    var countries = Connection.ctx.Countries.OrderBy(x => x.CountryName).ToList(); 
    ViewBag.Countries = new SelectList(countries, "CountryId", "CountryName"); 
    return View("Register", model); 
} 

はあなたのビューに再びモデルを渡す

+0

さて、私はそれをしましたが、問題は私のアクション "RegisterDo"がまったく引き起こされていないということです。:/ – User987

+0

UserRegistrationViewModelをビュー内のモデルとして渡しますか? –

+0

そうです。 – User987

1

すべては、それらのを見るために必要とするモデルを渡す

return View("Register", model); 

をする必要があり、他の部分を除くだけで結構ですエラーメッセージ。 そして、あなたのポストメソッドに[ActionName("Register")]属性を追加 "RegisterDo"

または

[ActionName("Register")] 
[HttpPost] 
public ActionResult RegisterDo(UserRegistrationViewModel model) 
+0

ポイントがあります。しかし、RegisterDoアクションのコードがまったくトリガされていません.../ – User987

+0

メソッドはまだ呼び出されていません... – User987

+0

View POSTメソッドを次のようにします:@using(Html.BeginForm( "Register "、" User "、FormMethod.Post)) アクションメソッド –

関連する問題