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