2012-04-05 6 views
7

Sto usando Ajax.Begin modulo nella mia MVC 3 applicazione + RazorAjax.BeginForm con OnBegin prevenire l'azione di essere chiamato

using (Ajax.BeginForm("ActionName", "ControllerName", new AjaxOptions { OnBegin = "ValidateDateFunction('" + @abc.xyz + "')", HttpMethod = "POST", UpdateTargetId = "savebutton" })) 
    { 
     <input type="submit" value="Save" /> 
    } 

Di seguito si descrive il mio metodo onBegin assomiglia. Sto passando un valore a questo metodo, sono in grado di ottenere un avviso adeguato.

function ValidateDateFunction(id) { 
     alert(id); 
     if(some-ConditionUsing-formId) 
     { 
      return false; 
     } 

     return true;   
    } 

Ora usando questo volevo che se la mia condizione fallisse, allora l'azione non dovrebbe essere chiamata. Ma qui nel mio caso in entrambe le condizioni viene chiamata la mia azione.

Aiutateci su questo.

Qui di seguito è il mio metodo validate effettivo

 function ValidateDateFunction(fId) { 

     var first = document.getElementById("startDate" + fId); 
     var second = document.getElementById("endDate" + fId); 

     if (first.value == "" && second.value != "") { 
      alert("Please select both dates"); 
      return false; 
     } 
     else if (first.value != "" && second.value == "") { 
      alert("Please select both dates"); 
      return false; 
     } 

     var startDateVal = new Date(first.value); 
     var endDateVal = new Date(second.value); 

     if (startDateVal.getTime() > endDateVal.getTime()) { 
      alert("Error ! The start date is after the end date!"); 
      return false; 
     } 
     alert('should not reach here'); 
     return true; 

    } 
+0

trovato! solo dovuto modificare la mia proprietà OnBegin a OnBegin = "tornare ValidateDateFunction ('" + @ abc.xyz + "')" link ho fatto riferimento http://stackoverflow.com/questions/8056968/asp -net-mvc-3-0-ajax-actionlink-onbeign-function-true-the-execute-the-action – Yasser

+0

cosa vuoi passare ad esso? – HaBo

+0

trovato http://stackoverflow.com/questions/10024135/ajax-beginform-with-onbegin-prevent-action-to-be-called/10039566#10039566 – Yasser

risposta

Problemi correlati