gpt4 book ai didi

jquery - ASP.NET MVC 4 : client validation not work

转载 作者:行者123 更新时间:2023-12-01 07:20:14 24 4
gpt4 key购买 nike

我有一个模型:

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; }

}

当用户单击编辑按钮时,我显示对话框:

$(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
});
});
});

编辑 View :

@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>

这是 Action :

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 { });
}

最佳答案

我认为在您的操作(在 Controller 中)之前添加 [ValidateAntiForgeryToken] 将解决您的问题

关于jquery - ASP.NET MVC 4 : client validation not work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14534631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com