0

I want to generate (or display hidden maybe?) different type of form depending on the selected Option in a DropDownList. What would be the best way to do that?

I have tried having in the base view named Create only a DropDownList with no forms initially, and script where on change using Ajax to some action in a controller which loads a partial view of the desired form display it as an HTML inside some <div> form container.

The problem with this is that I am not getting any client-side validation on the fields of the newly displayed form.

Javascript/jQuery code from Create view:

let dropDown = $("#SelectedExpenseType")
let expenseFormContainer = $("#ExpenseCreationForm")

dropDown.change(function () {
    let url = dropDown.val();
    //Check if selected option has NULL for Value Attribute
    if ($.isEmptyObject(url)) {
        expenseFormContainer.empty()
        return;
    }
    
    expenseFormContainer.load(url);
})
3
  • The problem with this is that I am not getting any client-side validation - this will depend on your client-side validation. It's likely that it initiates on doc.ready - you have two options - 1) using your existing code where the form is loaded dynamically, after the load make a call to the validation to reinitialise it. 2) load all the forms initially then show/hide them based on the drop down value (depends on the size of the forms) Commented Oct 17, 2023 at 15:36
  • @freedomn-m for option 1) how can I reinitialize the validation? Commented Oct 17, 2023 at 16:07
  • this will depend on your client-side validation Commented Oct 17, 2023 at 16:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.