次の場合、別の要素のクラスを変更することはでき!ModelState.IsValid
ますか?
以下に簡略化した私の見解は、各フィールドを<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);
}
モデル内の無効なプロパティのクラスerror
を追加できるようにしたいと思います。div.control
私はajax成功イベントでjQueryを介してこれを処理できると思いますが、おそらく一緒にもっと良い解決策があります。