开发者

In MVC, how can I make a field in a model non-required after a checkbox is checked?

开发者 https://www.devze.com 2023-02-08 23:26 出处:网络
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:

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消