2017-04-01 20 views
0

tuplesを使用して複数のモデルをビューにロードしています。コントローラはASP.NET MVC検証エラーが表示されない

 [HttpPost] 
     [ValidateAntiForgeryToken] 
     public async Task<PartialViewResult> Submit([Bind(Prefix = "Item2")] ContactForm model) 
     { 
      bool isMessageSent = true; 

      if (ModelState.IsValid) 
      { 
       try 
       { 
        await Services.EmailService.SendContactForm(model); 
       } 
       catch (Exception ex) 
       { 
        isMessageSent = false; 
       } 
      } 
      else 
      { 
       isMessageSent = false; 
      } 
      return PartialView("_SubmitMessage", isMessageSent); 
     } 

され、最終的に部分図が

@model bool 

@if (Model) 
{ 
    <div style="color:#c2ff1f">Your message has been sent.</div> 
} 
else 
{ 
    <div style="color:#f34141">An error occured while sending your message</div> 
} 
されたコンタクトフォームは

public class ContactForm 
    { 
     [Required(ErrorMessage = "You must provide your email."), EmailAddress] 
     public string Email { get; set; } 

     [Required(ErrorMessage = "You must include a message.")] 
     public string Message { get; set; } 
    } 

ある

@using (Ajax.BeginForm(null, null, ajaxOptions, new { @class = "contact-form" })) 

     { 
      @Html.AntiForgeryToken() 
      <div class="form-group form-group--xs"> 
       @Html.TextBoxFor(m => m.Item2.Email, new { @class = "form-control input-sm", placeholder = "Your email address..." }) 
       @Html.ValidationMessageFor(m => m.Item2.Email) 
      </div> 
      <div class="form-group form-group--xs"> 
       @Html.TextAreaFor(m => m.Item2.Message, new { @class = "form-control input-sm", placeholder = "Your message...", rows = "4" }) 
       @Html.ValidationMessageFor(m => m.Item2.Message) 
      </div> 

      <input type="submit" class="btn btn-primary-inverse btn-sm btn-block" value="Send Your Message" /> 
     } 

として

図であります10

更新:電子メールのテキストボックスのためにレンダリングされたHTMLがある

<input class="form-control input-sm" 
data-val="true" 
data-val-email="The Email field is not a valid e-mail address." 
data-val-required="You must provide your email." 
id="Item2_Email" 
name="Item2.Email" 
placeholder="Your email address..." 
type="text" 
value=""> 

私は、電子メールフィールドまたはメッセージフィールド内の任意のテキストを入力しない場合、メッセージは送信されません:)しかし、私は何の検証を持っていませんビューのエラー:( ここで何が間違っていますか?

+0

私は分かっていませんが、私はあなたがpartialviewの代わりにビューを返すと良いと思います。電子メールの送信中にエラーが発生したことが表示される画面が表示されます。私はあなたが "戻るView();を挿入すれば十分だと思うコントローラーのelseブロックに入力します。 – Larce

+0

@Larce公開非同期タスクに変更し、ビュー(モデル)を返します。それはまた、期待どおりに動作していない – OrElse

+0

何が起こっている?あなたはまだ部分的な見方になるのですか? – Larce

答えて

1

私は休憩が必要推測:

は、このコードを試してみてください。私は行方不明だった

@Scripts.Render("~/bundles/jqueryval") 
0

検証エラーメッセージコードを持たないpartialViewを返しています。

 [HttpPost] 
     [ValidateAntiForgeryToken] 
     public async Task<PartialViewResult> Submit([Bind(Prefix = "Item2")] ContactForm model) 
     { 
      bool isMessageSent = true; 

      if (ModelState.IsValid) 
      { 
       try 
       { 
        await Services.EmailService.SendContactForm(model); 
       } 
       catch (Exception ex) 
       { 
        isMessageSent = false; 
       } 

      } 
      else 
      { 
       isMessageSent = false; 
       //Return the view that has validation error message display code. 
       return View(model); 
      } 
      return PartialView("_SubmitMessage", isMessageSent); 
     } 
+0

こんにちは、PartialViewResultとViewResultの両方を同じコントローラから返すことはできますか? – OrElse

+1

actionResultを返す場合。はい! –

+0

更新を確認してください。 – OrElse

関連する問題