I have a page written using .NET MVC. In the model for a Person called PersonModel I have this defined which requires the user to enter some text in the last name field:
我有一个使用.NET MVC编写的页面。在名为PersonModel的Person的模型中,我定义了这个,它要求用户在姓氏字段中输入一些文本:
<DisplayName("Last Name"), Required()> _
Public Property LastName() As String
Get
Return _LastName
End Get
Set(ByVal value As String)
_LastName = value
End Set
End Property
On the form, there is a checkbox that a user can check to do some other things. Is there a way, using JQuery preferablly, to change that Last Name field to be non-Required? If not using JQuery I am open to other suggestions but since I am doing alot of things when this check box is checked anyways, I was hoping I could add this logic in there. Here is some sample of what I am doing when this box is checked to demonstrate...
在表单上,有一个用户可以检查以执行其他操作的复选框。有没有办法,使用JQuery优先,将Last Name字段更改为非必需?如果不使用JQuery,我会接受其他建议但是因为我在做这个复选框时做了很多事情,所以我希望我可以在那里添加这个逻辑。以下是我在检查此框以演示时所做的一些示例...
function doOwnerBusiness(event) {
function doOwnerBusiness(event){
if ($(this).is(':checked')) {
$('input[name="People_1__LastName"], label[for="People[1]_LastName"]').hide();
$("#People_1__LastName").hide();
$("#People_1__LastName").val("");
$("#People_1__LastName :input").attr('disabled', true);
$('input[name="People[1]_Suffix"], label[for="People[1]_Suffix"]').hide();
$("#People_1__Suffix").attr('disabled', true);
$('#People_1__Suffix')[0].selectedIndex = 0;
$('#People_1__Suffix').hide();
}
else {
$('input[name="People_1__LastName"], label[for="People[1]_LastName"]').show();
$("#People_1__LastName").show();
$('#People_1__LastName :input').attr('disabled', false);
}
}
Any help with this would be appreciated folks.
任何有关这方面的帮助都将受到赞赏。
Thank you
谢谢
William
威廉
Here is how I am declaring my checkbox and also part of the function where I am trying to check if it is checked or not...
这是我如何声明我的复选框以及我试图检查它是否被检查的函数的一部分...
<%=Html.CheckBoxFor(Function(model) model.FirstNameAsBusiness)%>
<%=Html.LabelFor(Function(model) model.FirstNameAsBusiness)%>
Function Nominate(ByVal m As NominationModel, ByVal captchaValid As Boolean) As ActionResult
If Not m.FirstNameAsBusiness.checked AndAlso String.IsNullOrEmpty(m.lastnametext) Then
ModelState.AddModelError("LastName", "Last Name field is required if you don't yada yada...")
Return View()
End If
1 个解决方案
#1
1
Short answer: no. You can't bypass the DataAnnotation with a jQuery call.
简答:不。您无法通过jQuery调用绕过DataAnnotation。
Technically, the Last Name field isn't required. So, I'd remove the DataAnnotation for Required, and then on the backend, when the user submits the form, verify that a field value exists when the checkbox isn't checked. If the conditional doesn't pass, and an error to ModelState for that field, and redirect to the page. (apologies for the c#):
从技术上讲,不需要姓氏字段。因此,我将删除DataAnnotation for Required,然后在后端,当用户提交表单时,验证未选中复选框时是否存在字段值。如果条件没有通过,并且该字段的ModelState出错,并重定向到该页面。 (对c#道歉):
public ActionResult Index(HomeIndexModel form)
{
if (!form.Checked && string.IsNullOrEmpty(form.LastName))
{
ModelState.AddModelError("LastName", "Last Name field is required if you don't yada yada...");
return View();
}
//conditional requirement passed...
}
If you want to get a little fancier, you can check out this thread, though all of the suggestions here are also server-side: ASP.NET MVC Conditional validation
如果你想获得一点点发烧友,你可以查看这个帖子,虽然这里的所有建议都是服务器端:ASP.NET MVC条件验证
#1
1
Short answer: no. You can't bypass the DataAnnotation with a jQuery call.
简答:不。您无法通过jQuery调用绕过DataAnnotation。
Technically, the Last Name field isn't required. So, I'd remove the DataAnnotation for Required, and then on the backend, when the user submits the form, verify that a field value exists when the checkbox isn't checked. If the conditional doesn't pass, and an error to ModelState for that field, and redirect to the page. (apologies for the c#):
从技术上讲,不需要姓氏字段。因此,我将删除DataAnnotation for Required,然后在后端,当用户提交表单时,验证未选中复选框时是否存在字段值。如果条件没有通过,并且该字段的ModelState出错,并重定向到该页面。 (对c#道歉):
public ActionResult Index(HomeIndexModel form)
{
if (!form.Checked && string.IsNullOrEmpty(form.LastName))
{
ModelState.AddModelError("LastName", "Last Name field is required if you don't yada yada...");
return View();
}
//conditional requirement passed...
}
If you want to get a little fancier, you can check out this thread, though all of the suggestions here are also server-side: ASP.NET MVC Conditional validation
如果你想获得一点点发烧友,你可以查看这个帖子,虽然这里的所有建议都是服务器端:ASP.NET MVC条件验证