0
!ModelState.IsValid
일 때 다른 요소의 클래스를 변경할 수 있습니까?ModelState가 유효하지 않을 때 다른 요소 조작
아래에 간단하게 내보기, ... <div class="control">
각 <input>
필드
@using(Html.BeginForm("AddRole","Admin", FormMethod.Post, new { @class = "ajax" })) {
<div class="control">
@Html.TextBoxFor(x => x.Role, new { @class = "input-xlarge", @placeholder = Html.DisplayNameFor(x => x.Role)})
@Html.ValidationMessageFor(x => x.Role)
</div>
}
$(function() {
$('form.ajax').submit(function() {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
if (result.success) {
location.reload(true);
} else {
$(this).html(result);
}
}
});
return false;
});
});
내 컨트롤러 감싸해야합니다
[HttpPost]
public ActionResult AddRole(RoleModel model) {
if(!ModelState.IsValid) {
return PartialView("_AddRolePartial", model);
}
try {
System.Web.Security.Roles.CreateRole(model.Role);
return Json(new {success = true});
} catch(Exception) {
ModelState.AddModelError("", "Role creation unsuccessful.");
}
return PartialView("_AddRolePartial", model);
}
I가 클래스
error
을 추가 할 수 있도록하고 싶습니다
div.control
유효하지 않은 모델 속성. 나는 아약스 성공 이벤트에서 jQuery를 통해 이것을 처리 할 수 있다고 생각하지만, 아마도 더 나은 해결책이 모두 함께있을 것입니다.