0

I have this in my view:

@using(Html.BeginForm())
{
    <label> Condition Target </label>
    <input type="text" name="ConditionTarget1" id="ConditionTarget1"/>
    <input type="text" name="ConditionTarget2" id="ConditionTarget2"/>
    <br/>

    <label> Condition Object </label>
    <select name="ConditionObject" id="ConditionObject">
        <option value="1"> Condition 1 </option>
        <option value="2"> Condition 2 </option>
    </select>
    <br/>

    <input type="submit"/>
}

In my controller (for example):

public ActionResult Validate(int value)
{
    return Json(value == 1, JsonRequestBehavior.AllowGet);
}

I want that when conditionobject changes, Validate server method should be called and depending on result conditiontarget text fields must become required (in this case if option 2 is selected). I tried jQuery remote validation but it validates select itself. Can you help me?

4

2 回答 2

0
 $('#ConditionObject').change(function(){
       $.ajax({
         type: "GET",
         url: "servercode.asp",
         data:this.val();
        success:function(data)
       {
         if(data==1)
        {
           $('#ConditionTarget1').addClass('required');//add required validation ,sample only
        }
      }
    });
 });

然后在验证检查 ConditionTarget1 有任何验证

$('input:submit').click(function(){

 if($('#ConditionTarget1').hasClass('required'))
  {
     alert('oops validation required');
      return false;
   }
});
于 2012-12-18T11:29:59.270 回答
0

你真的需要调用服务器端方法吗?尝试这个:

$('#ConditionObject').on("change",function(){
    if($(this).val() == 1)
    {
        $('#ConditionTarget1').addClass("Required");
    }
    else
    {
        $('#ConditionTarget1').removeClass("Required");
    }
});

提交时:<input id="submitForm" type="submit"/>

$("#submitForm").on("click", function(event){
  if($('#ConditionTarget1').hasClass("Required"))
    event.preventDefault();
});
于 2012-12-18T11:39:53.000 回答