모델을 내 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();
jQuery 선택기 '#form'이 (가) 유효하지 않습니다. 양식 인 요소 대신 ID가 "form"인 요소를 선택합니다. 해쉬없이'form'을 사용하십시오 –
유효성을 검사하지만 JS 오류를 throw하기 때문에 코드가 정확합니다 – AAlferez