Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否在第二页加载时保留服务器端验证?_Javascript_C#_Jquery_Html_Model View Controller - Fatal编程技术网

Javascript 是否在第二页加载时保留服务器端验证?

Javascript 是否在第二页加载时保留服务器端验证?,javascript,c#,jquery,html,model-view-controller,Javascript,C#,Jquery,Html,Model View Controller,编程新手遇到了一个问题。在填写我的表格时,在首选通信方式部分,当选中复选框“移动电话”时,“确认移动号码”文本框也会显示,因为我的JavaScript代码会动态显示确认文本框。如果我在“确认手机号码”文本框中输入与“手机”文本框不同的值并提交表单,则在“确认手机号码”文本框下触发验证,其中提到“请确保确认手机号码匹配” 但是,当用户提交表单时,文本框“确认手机号码”文本框将从屏幕上消失。由于服务器端验证的原因,在第二次加载表单时,“确认手机号码”文本框再次隐藏,如果我选择无线电“移动电话”,则“

编程新手遇到了一个问题。在填写我的表格时,在首选通信方式部分,当选中复选框“移动电话”时,“确认移动号码”文本框也会显示,因为我的JavaScript代码会动态显示确认文本框。如果我在“确认手机号码”文本框中输入与“手机”文本框不同的值并提交表单,则在“确认手机号码”文本框下触发验证,其中提到“请确保确认手机号码匹配”

但是,当用户提交表单时,文本框“确认手机号码”文本框将从屏幕上消失。由于服务器端验证的原因,在第二次加载表单时,“确认手机号码”文本框再次隐藏,如果我选择无线电“移动电话”,则“确认手机号码”文本框再次显示,但下面有验证消息

只是不明白为什么textbox不记得我的选中值来显示已验证的文本框,在表单提交后,我认为这是因为我的JavaScript。请提供帮助和建议

下面是我的带有服务器端验证代码的模型:

[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
}
[必需(ErrorMessage=“请选择首选通信方式选项”)]
公共交流?计算机通讯
{get;set;}
公共枚举通信
{
[显示(Name=“移动电话”,订单=0)]
电话号码
}
公共IEnumerable验证(ValidationContext ValidationContext)
{
if(this.ccommunication==Commmunication.TelephoneNo&&string.IsNullOrEmpty(this.MobileTelephoneNo))
{
返回新的ValidationResult(“请输入电话号码”,new[]{“MobileTelephoneNo”});//返回消息
}
//确认确认MobileTelephoneno匹配。
if(this.ccommunication==Commmunication.TelephoneNo&&ConfirmMobileTelephoneNo!=MobileTelephoneNo)
{
返回新的ValidationResult(“请确保确认手机号码匹配”,新[]{“ConfirmMobileTelephoneNo”});//返回消息
}
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>

@LabelFor(model=>model.MobileTelephoneNo,“输入您的移动电话号码:”,新{@style=“”,@class=“”,id=“”)
*
@Html.TextBoxFor(model=>model.MobileTelephoneNo,新建{placeholder=“输入您的手机号码”、@style=“”、@class=“表单控制”,id=“MobileTelephoneNo”})
@Html.ValidationMessageFor(model=>model.MobileTelephoneNo)
@LabelFor(model=>model.ConfirmMobileTelephoneNo,“确认您的移动电话号码:”,新的{@style=“”,@class=“”,id=“”)
*
@Html.TextBoxFor(model=>model.ConfirmMobileTelephoneNo,新的{placeholder=“重新输入您的手机号码”、@style=“”、@class=“表单控制”,id=“ConfirmMobileTelephoneNo”})
@Html.ValidationMessageFor(model=>model.ConfirmMobileTelephoneNo)
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>

$(文档).ready(函数(){
$('.communicationRB输入[name=ccomcommunication]')。单击(函数(){
if($(this.val()=“电话号码”){
$('.confirmmobtelno').show();//显示此文本框
$('.mobiletelredstar').show();
}否则{
$('.confirmmobtelno').hide();//隐藏文本框
$('.mobiletelredstar').hide();
}
});

您的问题就在这里。您的服务器正在接受您的验证信息,但没有被发送回您的客户端。您需要将该信息包含在返回到网页的响应中

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
    }

这个过程一开始可能很复杂,值得一读。

发布表单时,请确保在模型无效时将模型发送到视图。视图将从模型中获取值,进行渲染,然后将其发送回用户。此外,您没有正确执行服务器端验证。您正在使它变得更难。请阅读asp mvc中关于表单提交和验证的教程。我认为您不理解我的问题,验证后会再次隐藏一个用于验证的文本框。我理解您的问题。当用户发布表单时,它会击中您的控制器,如果您的控制器操作接受模型,则该模型需要有一个布尔值来承载您的复选框。如果无效,视图将使用该值并进行渲染。我完全理解您的问题。当我需要在复选框上再次显示文本框时,我的文本框在验证后再次隐藏selection@CodingYosh我想重新加载页面,根据所选的无线电输入显示相关的隐藏信息
return("View", model);