0

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:

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

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
tereško
  • 58,060
  • 25
  • 98
  • 150
Lee
  • 145
  • 1
  • 2
  • 9

1 Answers1

1

Short answer: no. You can't bypass the DataAnnotation with a jQuery call.

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#):

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

Community
  • 1
  • 1
Evan Nagle
  • 5,133
  • 1
  • 26
  • 24
  • Thanks for the advice. I will try this out tomorrow when I return to work. Best regards... – Lee Feb 03 '11 at 21:16
  • When trying to check the checkbox if it has been checked as mentioned above, it throws an error saying that "checked" is not a member of boolean. I am updating my post above to show what the part of my function is..... – Lee Feb 04 '11 at 14:56
  • Hey Lee -- I was assuming that you created a ViewModel called HomeIndexModel with a property called Checked, which was of a boolean type. Then, in your view, you'd inherit that ViewModel as the Model and create a field in your form using an HtmlHelper: Html.TextBoxFor(m => m.Checked). Check out: http://www.weirdlover.com/2010/07/01/the-big-boy-mvc-series-part-22-whoop/ – Evan Nagle Feb 04 '11 at 18:44