you can use this code to dyamically generate UI, implement Validation and posting form values to controller which can easily stored into database.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.js" integrity="sha512-8Z5++K1rB3U+USaLKG6oO8uWWBhdYsM3hmdirnOEWp8h2B1aOikj5zBzlXs8QOrvY9OxEnD2QDkbSKKpfqcIWw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<fieldset>
<legend></legend>
<form id="DemoForm">
<div id="divcust" class="container">
</div>
<br />
<br />
<div style="margin-left:30px">
<button type="button" name="Submit" onclick="SubmitData()" value="Submit" class="btn btn-success">Success</button>
<button type="button" name="Cancel" value="Submit" class="btn btn-danger">Cancel</button>
</div>
</form>
</fieldset>
<script>
var substr = {
"product": [
{ "Label": "First Name", "Id": "Name", "type": "text", "Required": "True" },
{ "Label": "Age", "Id": "Age", "type": "Number", "Required": "True" },
{ "Label": "Date of Birth", "Id": "DOB", "type": "date", "Required": "True" },
{ "Label": "Gender", "Id": "Gender", "type": "select", "Required": "True" },
{ "Label": "Mobile No", "Id": "Mobile_No", "type": "Number", "Required": "True" },
{ "Label": "Address 1", "Id": "Address1", "type": "text", "Required": "True" },
{ "Label": "Address 2", "Id": "Address2", "type": "text", "Required": "False" },
{ "Label": "Address 3", "Id": "Address3", "type": "text", "Required": "False" },
{ "Label": "City", "Id": "City", "type": "select", "Required": "True" }
],
"DropDown": [
{ "Key": "Select", "Value": "", "ParentId": "Gender" },
{ "Key": "Male", "Value": "Male", "ParentId": "Gender" },
{ "Key": "Female", "Value": "Female", "ParentId": "Gender" },
{ "Key": "Select", "Value": "", "ParentId": "City" },
{ "Key": "Mumbai", "Value": "Mumbai", "ParentId": "City" },
{ "Key": "Thane", "Value": "Thane", "ParentId": "City" },
{ "Key": "Pune", "Value": "Pune", "ParentId": "City" }
]
}
$(document).ready(function () {
AddCust();
CheckValidation();
});
function AddCust() {
var html = "";
for (var i = 0; i < substr.product.length; i++) {
if (substr.product[i].type == "select") {
var Dropdown_Values = substr.DropDown.filter(function (item) {
return item.ParentId == substr.product[i].Id;
});
var option = "";
Dropdown_Values.forEach(function (item) {
option = option + '<option \" class=\"form-control\"' + 'value=\"' + item.Value + '\"> ' +
item.Key + ' </option>'
})
html = html
+ '<div class="col-xs-4 col-md-4 col-sm-4"> <label for=\"CustList_' + substr.product[i].Id + '__Name\"> ' + substr.product[i].Label + ': <span id=\"Hide_' + substr.product[i].Id + '\" style="color:red" style="display:none" hidden> * </span></label>'
+ '<select id=\"' + substr.product[i].Id + '\" class=\"form-control\"' + 'name=\"' + substr.product[i].Id + '\"> ' +
option
+ ' </select></div>';
}
else {
html = html
+ '<div class="col-xs-4 col-md-4 col-sm-4"> <label for=\"CustList_' + substr.product[i].Id + '__Name\"> ' + substr.product[i].Label + ': <span id=\"Hide_' + substr.product[i].Id + '\" style="color:red" style="display:none" hidden> * </span></label>'
+ '<input id=\"' + substr.product[i].Id + '\" class=\"form-control\"' + ' type=\"'
+ substr.product[i].type + '\" value=\"\" name=\"' + substr.product[i].Id + '\"> </div>';
}
}
$('#divcust').append(html);
};
function SubmitData() {
var Required_Input = substr.product.filter(function (item) {
return item.Required == "True";
});
var $inputs = $('#DemoForm :input');
var Form_values = {};
$inputs.each(function () {
Form_values[this.name] = $(this).val();
});
delete Form_values.Submit;
delete Form_values.Cancel;
for (var i = 0; i < Required_Input.length; i++) {
var filtered_Key = "";
var filtered_Values = "";
var Required_alert = Required_Input[i].Label;
var Field = Required_Input[i].Id;
const filteredByKey = Object.fromEntries(Object.entries(Form_values).filter(([key, value]) => key === Field))
for (var key in filteredByKey) {
filtered_Key = key;
filtered_Values = filteredByKey[key];
}
if (filtered_Key == Required_Input[i].Id) {
if (filtered_Values == "") {
alert(Required_alert + " is Required ")
return false;
}
}
}
Save_Data(Form_values);
}
function CheckValidation() {
var Required_Input = substr.product.filter(function (item) {
return item.Required == "True";
});
for (var i = 0; i < Required_Input.length; i++) {
$("#Hide_" + Required_Input[i].Id).show();
}
}
function Save_Data(Data) {
var Common_Set = {};
Common_Set.Json_Input = JSON.stringify(Data);
$.ajax({
url: "/UserMaster/Save_Data",
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(Common_Set),
success: function (result) {
// when call is sucessfull
alert(JSON.stringify(Data))
window.location.reload();
},
error: function (err) {
// check the err for error details
}
});
}
</script>