2013-06-11 2 views
0

모델을 내 Action으로 보내기 전에 일부 필드의 유효성을 검사하고 싶습니다.ajax.post를 호출 할 때 유효성 검사 유형이 이루어지지 않습니다.

$.post('@Url.Action("AddUpdateConfigs")', 
    {QueueMonitorConfigurationsID: ident, QueueMonitorConfigTypeName: $('#ddlConfigTypeName').val(), Threshold:$('#ddlThreshold').val(), QueueMonitorValueTypeName:$('#ddlValueTypeName').val(), Location: $('#txtbLocation').val(), Value: $('#txtbLimit').val()}, 
    function(data){ 
     if (!data.Success){ 
      alert(data.Description); 
     } 
     else{ 
      //$('#gridView').load('/Storage/gvConfigurations'); 
      $.get('@Url.Action("gvConfigurations", "Storage")',null,function(data){$('#gridView').html(data);},'html'); 
     } 
    },'json'); 

그리고 호출 기능 :

public JsonResult AddUpdateConfigs(StorageConfigurationModel modelbind) 
{ 
    //Take the list of configurations 
    IEnumerable<StorageConfigurationModel> configList = (IEnumerable<StorageConfigurationModel>)Session["ConfigurationList"]; 
    //Loop 
    foreach (StorageConfigurationModel configModel in configList) 
    { 
     //Is it a duplicated entry? 
     if ((configModel.QueueMonitorConfigTypeName == modelbind.QueueMonitorConfigTypeName) && (configModel.Location == modelbind.Location) && (configModel.QueueMonitorValueTypeName == modelbind.QueueMonitorValueTypeName) && (configModel.Threshold == modelbind.Threshold)) 
     { 
      //Found duplicated entry 
      return Json(new { Success = false, Description = "Duplicate entry" }); 
     } 
    } 
    //If not duplicated, add it to DB 

    try 
    { 
     if (modelbind.Location.StartsWith("\\")) 
     { 
      DirectoryInfo dir = new DirectoryInfo(modelbind.Location); 
      if (dir.Exists) 
      { 
       int finalValue = 0; 
       int pathInt = 0; 
       int valueTypeInt = 0; 

       if (modelbind.QueueMonitorConfigTypeName == PathType.Path) 
        pathInt = 1; 
       else 
        pathInt = 2; 
       switch (modelbind.QueueMonitorValueTypeName) 
       { 
        case UnitType.Percentage: 
         valueTypeInt = 1; 
         break; 
        case UnitType.MB: 
         valueTypeInt = 2; 
         break; 
        case UnitType.GB: 
         valueTypeInt = 3; 
         break; 
        case UnitType.TB: 
         valueTypeInt = 4; 
         break; 
        case UnitType.Files: 
         valueTypeInt = 5; 
         break; 
       } 

       if (modelbind.Threshold == ThresholdType.Upper) 
        finalValue = modelbind.Value; 
       else 
        finalValue = Convert.ToInt32("-" + modelbind.Value); 
       Boolean result = false; 
       result = DAL.queryAddUpdateConfig(modelbind.QueueMonitorConfigurationsID, pathInt, modelbind.Location, finalValue, valueTypeInt); 
       return Json(new { Success = result, Description = (result) ? ((modelbind.QueueMonitorConfigurationsID == -1) ? "New data inserted" : "Data updated") : "Error in Data types" }); 

      } 
      else 
      { 
       return Json(new { Success = false, Description = "Location Path is not correct" }); 
      } 
     } 
     else 
     { 
      return Json(new { Success = false, Description = "Location Path has to be UNC path" }); 
     } 
    } 
    catch (Exception j) 
    { 
     return Json(new { Success = false, Description = "Error: " + j }); 
    } 
} 

그것은 빙에 똑똑

내가

@Html.TextBoxFor(cModel => cModel.Value, new { id = "txtbLimit", @type = "int" }) 
@Html.ValidationMessageFor(cModel => cModel.Value) 

을 검증 할 textboxfor 그리고 아약스 게시물입니다 모델이지만 유효성 확인을하지는 않습니다. 값 (int)이 있어야하는 곳에 텍스트 상자에 문자열을 넣으면 유효성 검사를하지 않고 0으로 변환합니다.

또한 정규 표현식을 사용하여 위치에 대한 유효성을 검사했습니다. 다시 작동하지 않습니다.

누구든지 잘못된 것이 있습니까? 주셔서 감사합니다

편집 :

내가 가진 :

@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "form" })) 
    {} 

그리고이 아약스 게시하기 전에 :

$('#form').validate(); 
+0

jQuery 선택기 '#form'이 (가) 유효하지 않습니다. 양식 인 요소 대신 ID가 "form"인 요소를 선택합니다. 해쉬없이'form'을 사용하십시오 –

+0

유효성을 검사하지만 JS 오류를 throw하기 때문에 코드가 정확합니다 – AAlferez

답변

0

내가 ModelState.IsValid 경우 코드에서 확인 표시되지 않습니다. 또한 당신은 클라이언트 측에서 양식을 확인하거나 Ajax.BeginForm를 사용하는

$('form').validate(); 

를 호출해야합니다.

+0

그랬지만 js 오류가 발생했습니다 : 'SCRIPT5007 : 속성 'settings'값을 가져올 수 없습니다 : 객체가 null이거나 정의되지 않았습니다. ' – AAlferez

+0

필드가 폼으로 싸여 있습니까? 클라이언트 측 유효성 검사를 원한다면 필드를'Html.BeginFrom' 또는'Ajax.BeginForm'으로 포장해야합니다. 그렇지 않으면 직접 jQuery 유효성 검사를 구성해야합니다. –

+0

내 편집 참조; 내가 가지고 있지만 JS 오류가 발생했습니다. – AAlferez