1
MVC에서 기본 제공 유효성 검사를 사용하려고하는데 작동하지 않는 것 같습니다. 내 양식의 필드를 비워두면 "Successfully Saved"라는 메시지가 나타납니다. 메시지.팝업 창에서 MVC 유효성 검사가 예상대로 작동하지 않습니다.
양식에 '필수'로 표시된 필드를 필수로 표시해서는 안됩니까?
컨트롤러 :
public ActionResult Create([DataSourceRequest] DataSourceRequest request, ACore.Asset assetForm)
{
var results = new
{
value = false,
Message = ""
};
if (ModelState.IsValid)
{
results = new
{
value = true,
Message = "Successfully Saved."
};
return Json(results);
}
results = new
{
value = false,
Message = "Please check the form values."
};
return Json(results);
}
보기 (응축) :
@using (Html.BeginForm("Create", "Asset", FormMethod.Post, new { id = "frmAsset"}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="tempStyle">
<div class="editor-label fl">
@Html.LabelFor(model => model.AssetName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AssetName)
@Html.ValidationMessageFor(model => model.AssetName)
</div>
</div>
<div style="position: relative;">
<input type="button" value="Save" id="btnSave" />
</div>
자바 스크립트 처리 내 저장 :
var saveAsset = function (e) {
var form = $("#frmAsset");
$.ajax({
type: "POST",
url: "/Asset/Create",
data: $(form).serialize(),
success: function (data) {
if (data.value == true) {
alert(data.Message);
// Close popup window
var window = $('#AssetEditorPopUp').data("kendoWindow");
window.close();
// Refresh grid to show changes
$('#grid').data("kendoGrid").dataSource.read();
return;
}
alert(data.Message);
},
error: function() {
alert("There was an error editing the asset.");
}
});
};
모델 :
public class Asset
{
[ScaffoldColumn(false)]
public int AssetId { get; set; }
[Required]
[Display(Name="Asset Name:")]
public string AssetName { get; set; }
public string AssetFormerName { get; set; }
public string Seg1Code { get; set; }
public string Seg3Code { get; set; }
public bool? ActiveFlag { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string AssetType { get; set; }
[Display(Name = "ZipCode:")]
[RegularExpression("([a-zA-Z0-9 .&'-]+)", ErrorMessage = "Enter only alphabets and numbers of First Name")]
public string ZipCode { get; set; }
}
이것은 내가 아약스 대신 POST를 수행하는 것입니다.
흥미로운 문제. 관심사로 클라이언트 유효성 검사를 사용할 수 있습니까? web.config에서 ClientValidationEnabled를 확인하십시오. –
Ajax를 사용하는 대신 일반 POST를 수행하면 유효성 검사가 작동합니까? –
내 web.config에서 클라이언트 유효성 검사가 사용됩니다. 정상적인 POST를 시도하고 여기에 다시 게시합니다. – Mithrilhall