2017-05-27 2 views
0

プログラミングに新しい問題がありました。私のフォームを完了している間、優先通信方法セクションで、「携帯電話」チェックボックスを選択すると、JavaScriptコードがテキストボックスを動的に確認するため、「携帯番号の確認」テキストボックスも表示されます。 「携帯電話番号の確認」テキストボックスに「携帯電話番号」テキストボックスと異なる値を入力してフォームを送信すると、「携帯電話番号の確認」テキストボックスに確認が送られます。2ページ目の読み込み時にサーバーサイド検証を保持しますか?

ただし、ユーザーがフォームを使用しているときに、テキストボックス「モバイル番号の確認」テキストボックスが画面から消えます。サーバー側の検証のために、フォームが2回目に読み込まれたときに「携帯番号の確認」テキストボックスが再び表示されず、「携帯電話」ラジオを選択した場合は「携帯番号の確認」テキストボックスに再度表示されます。それ。

なぜ、テキストボックスで検証済みの値が覚えておらず、検証済みのテキストボックスが表示されないのか混乱しています。

[Required(ErrorMessage = "Please select preferred way of communication option.")] 
public Commmunication? CCommmunication 
{ get; set; } 

public enum Commmunication 
{ 

[Display(Name = "Mobile telephone", Order = 0)] 
TelephoneNo 
} 


public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) 
{ 
if (this.CCommmunication == Commmunication.TelephoneNo && string.IsNullOrEmpty(this.MobileTelephoneNo)) 
{ 
yield return new ValidationResult("Please enter telephone number", new[] { "MobileTelephoneNo" }); //returns message 
} 
// Confirm ConfirmMobileTelephoneNo matches. 
if (this.CCommmunication == Commmunication.TelephoneNo && ConfirmMobileTelephoneNo != MobileTelephoneNo) 
{ 
yield return new ValidationResult("Please ensure confirm mobile number matches", new[] { "ConfirmMobileTelephoneNo" }); //returns message 
} 

2:マイビューのHTMLコード

<div class="col-md-6"> 
<!-- <i class="fa fa-phone-square" aria-hidden="true"></i>--> 
@Html.LabelFor(model => model.MobileTelephoneNo, "Type in your mobile telephone no:", new { @style = "", @class = "", id = "" }) 
<span class="mobiletelredstar" style="display:none">*</span> 
@Html.TextBoxFor(model => model.MobileTelephoneNo, new { placeholder = "Enter your mobile no", @style = "", @class = "form-control", id = "MobileTelephoneNo" }) 
@Html.ValidationMessageFor(model => model.MobileTelephoneNo) 
</div> 

<div id="ConfirmMobTelNo" class="confirmmobtelno col-md-6" style="display:none"> 
<!-- <i class="fa fa-phone-square" aria-hidden="true"></i>--> 
@Html.LabelFor(model => model.ConfirmMobileTelephoneNo, "Confirm your mobile telephone no:", new { @style = "", @class = "", id = "" }) 
<span class="mobiletelredstar" style="display:none">*</span> 
@Html.TextBoxFor(model => model.ConfirmMobileTelephoneNo, new { placeholder = "Re-enter your mobile no", @style = "", @class = "form-control", id = "ConfirmMobileTelephoneNo" }) 
@Html.ValidationMessageFor(model => model.ConfirmMobileTelephoneNo) 
</div> 

3:私のJavaScriptコード:

<script> 

$(document).ready(function() { 

$('.communicationRB input[name=CCommmunication]').click(function() { 
if ($(this).val() == "TelephoneNo") { 
$('.confirmmobtelno').show(); //show this text box 
$('.mobiletelredstar').show(); 
} else { 
$('.confirmmobtelno').hide(); //hide textbox 
$('.mobiletelredstar').hide(); 
} 
}); 
</script> 
+1

フォームが転記されるときに、モデルが有効でない場合は、モデルをビューに送信してください。ビューはモデルから値を取得し、レンダリングし、ユーザーに返されます。また、サーバー側の検証も適切に行っていません。あなたはそれをより困難にしています。 asp mvcでフォームの提出と検証に関するチュートリアルを読んでください。 – CodingYoshi

+0

私はあなたが私の質問を理解したとは思わない、妥当性確認の後に再び検証されるテキストボックス。 –

+0

私はあなたの質問を理解しました。ユーザーがコントローラにヒットしたフォームを投稿し、コントローラアクションがモデルを受け入れる場合、そのモデルはチェックボックスの状態を保持するブール値を持つ必要があります。有効でない場合、ビューはその値を使用しレンダリングされます。 – CodingYoshi

答えて

0
以下

サーバー側の検証コードと私のモデルであるのヘルプとアドバイスしてください

あなたの問題はここにあります。サーバーが検証情報を受け入れていますが、クライアントに返信されません。その情報をあなたのウェブページに返す必要があります。私の経験で

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) 
    { 
    if (this.CCommmunication == Commmunication.TelephoneNo && string.IsNullOrEmpty(this.MobileTelephoneNo)) 
    { 
    yield return new ValidationResult("Please enter telephone number", new[] { "MobileTelephoneNo" }); //returns message 
    } 
    // Confirm ConfirmMobileTelephoneNo matches. 
    if (this.CCommmunication == Commmunication.TelephoneNo && ConfirmMobileTelephoneNo != MobileTelephoneNo) 
    { 
    yield return new ValidationResult("Please ensure confirm mobile number matches", new[] { "ConfirmMobileTelephoneNo" }); //returns message 
    } 

、我々は通常modelsと呼ばれるクラスを使用してこの問題を解決します。検証情報はそのオブジェクト内に含まれ、Webページを送信したのと同じ応答で返されます。例:

return("View", model); 

このプロセスは最初は複雑になる可能性があります。

関連する問題