私はモデルを持っています:
public class UserInfoViewModel
{
public int Id { get; set; }
[LocalizedDisplayName("CarNumber", NameResourceType = typeof(CabinetViewModelsStrings))]
[Required]
public string CarNumber { get; set; }
[LocalizedDisplayName("CarMark", NameResourceType = typeof(CabinetViewModelsStrings))]
public string CarMark { get; set; }
[LocalizedDisplayName("CarYear", NameResourceType = typeof(CabinetViewModelsStrings))]
[IntLengthAttribute(4, ErrorMessage = "Wrong Year")]
public int CarYear { get; set; }
[LocalizedDisplayName("Email", NameResourceType = typeof(CabinetViewModelsStrings))]
public string Email { get; set; }
[LocalizedDisplayName("PhoneNumber", NameResourceType = typeof(CabinetViewModelsStrings))]
public string PhoneNumber { get; set; }
[LocalizedDisplayName("FullName", NameResourceType = typeof(CabinetViewModelsStrings))]
public string FullName { get; set; }
[LocalizedDisplayName("FirstName", NameResourceType = typeof(CabinetViewModelsStrings))]
public string FirstName { get; set; }
[LocalizedDisplayName("LastName", NameResourceType = typeof(CabinetViewModelsStrings))]
public string LastName { get; set; }
[LocalizedDisplayName("MiddleName", NameResourceType = typeof(CabinetViewModelsStrings))]
public string MiddleName { get; set; }
}
ユーザーがEdit
ボタンをクリックすると、ダイアログが表示されます:
$(document).on('click', '#edit_info', function() {
var dialog = createDialog();
var editUrl = '/cabinet/edit';
dialog.load(editUrl, function(content) {
dialog.dialog({
close: function(event, ui) {
dialog.remove();
},
modal: true,
width: 460,
resizable: false
});
});
});
Edit
ビュー:
@using (Ajax.BeginForm("Edit", "Cabinet", new AjaxOptions { HttpMethod = "POST" }, new { Id = "update_user_info" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>@CabinetViewsStrings.FormEditInfo</legend>
@Html.HiddenFor(m=>m.Id)
<ol>
<li>
@Html.LabelFor(m => m.FirstName)
@Html.TextBoxFor(m => m.FirstName)
</li>
<li>
@Html.LabelFor(m => m.MiddleName)
@Html.TextBoxFor(m => m.MiddleName)
</li>
<li>
@Html.LabelFor(m => m.LastName)
@Html.TextBoxFor(m => m.LastName)
</li>
<li>
@Html.LabelFor(m => m.Email)
@Html.TextBoxFor(m => m.Email)
</li>
<li>
@Html.LabelFor(m => m.PhoneNumber)
@Html.TextBoxFor(m => m.PhoneNumber)
</li>
<li>
@Html.LabelFor(m => m.CarNumber)
@Html.TextBoxFor(m => m.CarNumber)
@Html.ValidationMessageFor(model => model.CarNumber)
</li>
<li>
@Html.LabelFor(m => m.CarMark)
@Html.TextBoxFor(m => m.CarMark)
</li>
<li>
@Html.LabelFor(m => m.CarYear)
@Html.TextBoxFor(m => m.CarYear)
@Html.ValidationMessageFor(model => model.CarYear)
</li>
</ol>
<input id="update_user_info" type="submit" value="@CabinetViewsStrings.ButtonSaveEditInfo" />
</fieldset>
}
@Scripts.Render("~/bundles/jqueryval")
しかし、空のCarNumber
フィールドで sumbit ボタンをクリックしても、エラーは発生しません。エラーはどこにありますか?
必要な js ライブラリがロードされます。
GET jquery.unobtrusive-ajax.js?_=1359182513969
GET jquery.validate.js?_=1359182514123
GET jquery.validate.unobtrusive.js?_=1359182514379
生成された html のこの部分:
<li>
<label for="CarNumber">Car Number</label>
<input type="text" value="B650LZN" name="CarNumber" id="CarNumber" data-val-required="The Car Number field is required." data-val="true">
</li>
これはアクションです:
public ActionResult Edit()
{
var model = _viewModelCreator.Create<UserInfoViewModel, User>(PcpSession.CurrentUser);
return PartialView(model);
}
[HttpPost]
public ActionResult Edit(UserInfoViewModel userInfo)
{
if (!ModelState.IsValid)
{
return View(userInfo);
}
var user = _entityCreator.Create<User, UserInfoViewModel>(userInfo);
_userService.UpdateUser(user);
PcpSession.CurrentUser = user;
return Json(new { });
}