Example of Custom Components in ASP.NET MVC Form Renderer Control
This sample demonstrates how to render custom components for individual form field components in the Form Renderer control. Registered components replace the built-in form components with custom or third-party UI components, enabling greater flexibility and customization.
Contact Us
We would love to hear from you. Please fill out the form below and our team will get back to you.
@Html.UiFormRenderer(new UiFormRendererOptions { Id = "form-renderer-control", Columns = 1, SubmitText = "Submit", OnSubmit = "onFormSubmit", Fields = (List<UiFormField>)ViewData["contactSchema"] })
<div class="fr-custom control-section">
<div class="fr-contact">
<div class="form-heading">
<h2>Contact Us</h2>
<p>We would love to hear from you. Please fill out the form below and our team will get back to you.</p>
</div>
@Html.UiFormRenderer(new UiFormRendererOptions { Id = "form-renderer-control", Columns = 1, SubmitText = "Submit", OnSubmit = "onFormSubmit", Fields = (List<UiFormField>)ViewData["contactSchema"] })
<div id="fr-result" class="fr-result" role="status" aria-live="polite"></div>
</div>
</div>
<style>
.fr-custom .fr-contact { max-width: 560px; margin: 0 auto; padding: 16px 0; }
.fr-custom .form-heading { text-align: center; padding: 12px 0; }
.fr-custom .fr-input, .fr-custom .fr-dropdown, .fr-custom .fr-textarea { box-sizing: border-box; width: 100%; padding: 6px 8px; border: 1px solid var(--ui-input-border); border-radius: 5px; background: inherit; color: inherit; font: inherit; }
.fr-custom .fr-dropdown { height: 32px; }
.fr-custom .fr-check { display: flex; align-items: center; gap: 10px; font-size: 14px; font-weight: 500; }
.fr-custom .fr-check input { width: 15px; height: 15px; }
.fr-custom .fr-result { margin-top: 14px; font-size: 13px; }
</style>
<script>
// Syncfusion: the text box, e-mail, drop-down, text area and check box fields are drawn from HTML templates; here each is a component the page registers
window.addEventListener('load', function () {
'use strict';
function control(tag, className, field, host) {
var el = document.createElement(tag);
el.className = className;
el.id = field.id + '_control';
el.name = field.name;
if (field.options.Placeholder || field.options.placeholder) { el.placeholder = field.options.Placeholder || field.options.placeholder; }
host.appendChild(el);
el.addEventListener('input', function () { field.changed(); });
el.addEventListener('change', function () { field.changed(); });
return el;
}
function text(type) {
return {
render: function (field, host) {
var el = control('input', 'fr-input', field, host);
el.type = type;
return { getValue: function () { return el.value === '' ? null : el.value; }, setValue: function (v) { el.value = v === null || v === undefined ? '' : v; } };
}
};
}
var forms = window.Lumen.formRenderer;
forms.register('customText', text('text'));
forms.register('customEmail', text('email'));
forms.register('customTextarea', {
render: function (field, host) {
var el = control('textarea', 'fr-textarea', field, host);
el.rows = field.options.Rows || 4;
return { getValue: function () { return el.value === '' ? null : el.value; }, setValue: function (v) { el.value = v === null || v === undefined ? '' : v; } };
}
});
forms.register('customDropdown', {
render: function (field, host) {
var el = control('select', 'fr-dropdown', field, host);
var empty = document.createElement('option');
empty.value = '';
empty.textContent = field.options.Placeholder || '';
el.appendChild(empty);
(field.options.Items || field.options.items || []).forEach(function (item) {
var option = document.createElement('option');
option.value = item.Value || item.value;
option.textContent = item.Text || item.text;
el.appendChild(option);
});
return { getValue: function () { return el.value === '' ? null : el.value; }, setValue: function (v) { el.value = v === null || v === undefined ? '' : v; } };
}
});
forms.register('customCheckbox', {
render: function (field, host) {
var label = document.createElement('label');
label.className = 'fr-check';
var el = document.createElement('input');
el.type = 'checkbox';
el.id = field.id + '_control';
el.name = field.name;
label.appendChild(el);
label.appendChild(document.createTextNode(field.options.Text || field.options.text || field.label));
host.appendChild(label);
el.addEventListener('change', function () { field.changed(); });
return { getValue: function () { return el.checked ? true : null; }, setValue: function (v) { el.checked = !!v; } };
}
});
});
// Submit shows what was entered, instead of posting it
function onFormSubmit(args) {
document.getElementById('fr-result').textContent = 'Submitted: ' + JSON.stringify(args.values);
args.cancel = true;
}
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class FormRendererController : Controller
{
// The page is Views/FormRenderer/Custom.cshtml; it draws what ViewData holds.
public ActionResult Custom()
{
ViewData["contactSchema"] = new FormRendererData().GetContactForm();
return View();
}
/// <summary>The form definitions of the samples, as Syncfusion's FormRendererData holds them (fields with their labels, rules and choices).</summary>
public class FormRendererData
{
private static List<UiListItem> Items(params string[] values)
{
return values.Select(v => new UiListItem { Value = v, Text = v }).ToList();
}
/// <summary>The User Registration form: personal information and account details.</summary>
public List<UiFormField> GetUserRegistration()
{
return new List<UiFormField>
{
new UiFormField { Name = "FirstName", Label = "First Name", Required = true },
new UiFormField { Name = "LastName", Label = "Last Name", Required = true },
new UiFormField { Name = "PhoneNumber", Label = "Phone Number", Type = UiFormFieldType.Number, Placeholder = "Enter your phone number", Options = new { ShowSpinButton = false, Format = "N0" } },
new UiFormField { Name = "UserName", Label = "User Name", Required = true },
new UiFormField { Name = "Password", Label = "Password", Type = UiFormFieldType.Password, Required = true },
new UiFormField { Name = "ConfirmPassword", Label = "Confirm Password", Type = UiFormFieldType.Password, Required = true },
new UiFormField { Name = "Terms", Label = "I agree to the Terms and Conditions", Type = UiFormFieldType.CheckBox, Columns = 12 }
};
}
/// <summary>The Customer Service form: the customer and the request (the category follows the request type).</summary>
public List<UiFormField> GetCustomerService()
{
return new List<UiFormField>
{
new UiFormField { Name = "CustomerName", Label = "Customer Name", Required = true, Placeholder = "Enter your full name" },
new UiFormField { Name = "EmailAddress", Label = "Email Address", Type = UiFormFieldType.Email, Placeholder = "Enter your email" },
new UiFormField { Name = "PhoneNumber", Label = "Phone Number", Type = UiFormFieldType.Number, Placeholder = "Enter your phone number", Options = new { ShowSpinButton = false, Format = "N0" } },
new UiFormField { Name = "RequestType", Label = "Request Type", Type = UiFormFieldType.DropDown, Options = new { OnChange = "requestTypeChange" }, Required = true, Placeholder = "Enter the request type",
Items = Items("Technical Support", "Billing", "General Inquiry", "Feature Request") },
new UiFormField { Name = "Category", Label = "Category", Type = UiFormFieldType.DropDown, Required = true, Placeholder = "Choose a request type first", Disabled = true,
Items = Items("Bug", "Configuration", "Performance", "Access Issue", "Invoice", "Payment Failure", "Refund", "Subscription", "New Feature", "Enhancement", "Integration", "Product Information", "Pricing", "Documentation") },
new UiFormField { Name = "Priority", Label = "Priority", Type = UiFormFieldType.DropDown, Required = true, Placeholder = "Enter the priority of the issue",
Items = Items("Low", "Medium", "High", "Critical") },
new UiFormField { Name = "Description", Label = "Description", Type = UiFormFieldType.TextArea, Required = true, Placeholder = "Enter the description", Columns = 12 }
};
}
/// <summary>The Doctor Appointment form: the patient, the appointment and the healthcare information (the doctor follows the department; the insurance fields follow the answer).</summary>
public List<UiFormField> GetDoctorsAppointment()
{
return new List<UiFormField>
{
new UiFormField { Name = "PatientName", Label = "Patient Name", Required = true },
new UiFormField { Name = "EmailAddress", Label = "Email Address", Type = UiFormFieldType.Email, Required = true },
new UiFormField { Name = "MobileNumber", Label = "Mobile Number", Required = true },
new UiFormField { Name = "Gender", Label = "Gender", Type = UiFormFieldType.DropDown, Required = true, Items = Items("Male", "Female", "Other") },
new UiFormField { Name = "Age", Label = "Age", Type = UiFormFieldType.Number, Options = new { Min = 0, Max = 120, Format = "N0" } },
new UiFormField { Name = "DateOfBirth", Label = "Date of Birth", Type = UiFormFieldType.Date, Required = true },
new UiFormField { Name = "Department", Label = "Department", Type = UiFormFieldType.DropDown, Options = new { OnChange = "departmentChange" }, Required = true, Items = Items("Cardiology", "Dermatology", "Pediatrics", "Orthopedics") },
new UiFormField { Name = "Doctor", Label = "Doctor", Type = UiFormFieldType.DropDown, Required = true, Disabled = true, Placeholder = "Choose a department first",
Items = Items("Dr. John Carter", "Dr. Emily Smith", "Dr. Sarah Wilson", "Dr. Michael Brown", "Dr. David Lee", "Dr. Lisa Thomas", "Dr. Robert Taylor", "Dr. James Walker") },
new UiFormField { Name = "AppointmentDate", Label = "Appointment Date", Type = UiFormFieldType.Date, Required = true },
new UiFormField { Name = "AppointmentTime", Label = "Appointment Time", Type = UiFormFieldType.DropDown, Required = true, Items = Items("09:00 AM", "10:00 AM", "11:00 AM", "02:00 PM", "03:00 PM", "04:00 PM") },
new UiFormField { Name = "VisitType", Label = "Visit Type", Type = UiFormFieldType.Radio, Required = true, Columns = 12, Items = Items("Consultation", "Follow-up", "Routine Checkup", "Emergency") },
new UiFormField { Name = "ReasonForVisit", Label = "Reason for Visit", Type = UiFormFieldType.TextArea, Required = true, Columns = 12 },
new UiFormField { Name = "HasInsurance", Label = "Has Insurance", Type = UiFormFieldType.Radio, Options = new { OnChange = "insuranceChange" }, Columns = 12, Items = Items("Yes", "No") },
new UiFormField { Name = "InsuranceProvider", Label = "Insurance Provider", Required = true },
new UiFormField { Name = "PolicyNumber", Label = "Policy Number", Required = true }
};
}
/// <summary>The Contact Us form of the Custom Components sample.</summary>
public List<UiFormField> GetContactForm()
{
return new List<UiFormField>
{
new UiFormField { Name = "fullName", Label = "Full Name", Type = UiFormFieldType.Custom, Component = "customText", Required = true, Options = new { Placeholder = "Enter your full name" }, Validation = new UiFormValidation { MinLength = 2, MaxLength = 100 }, Columns = 12 },
new UiFormField { Name = "email", Label = "Email Address", Type = UiFormFieldType.Custom, Component = "customEmail", Required = true, Options = new { Placeholder = "Enter your email address" }, Columns = 12 },
new UiFormField { Name = "inquiryType", Label = "Inquiry Type", Type = UiFormFieldType.Custom, Component = "customDropdown", Columns = 12,
Options = new { Placeholder = "Select inquiry type", Items = new[] { new { Text = "General Inquiry", Value = "general" }, new { Text = "Sales", Value = "sales" }, new { Text = "Support", Value = "support" }, new { Text = "Partnership", Value = "partnership" }, new { Text = "Feedback", Value = "feedback" } } } },
new UiFormField { Name = "message", Label = "Message", Type = UiFormFieldType.Custom, Component = "customTextarea", Required = true, Options = new { Placeholder = "Enter your message", Rows = 5 }, Validation = new UiFormValidation { MinLength = 10, MaxLength = 1000 }, Columns = 12 },
new UiFormField { Name = "consent", Label = "Consent", Type = UiFormFieldType.Custom, Component = "customCheckbox", Required = true, Options = new { Text = "I agree to be contacted regarding my inquiry." }, Columns = 12 }
};
}
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
In this sample, a "Contact Us" form is rendered using the Form Renderer control. The text box, text area, drop-down list, and check box fields are customized with components the page registers.
Users can interactively fill out the form, benefit from built-in validation, and submit their responses for data collection.
Design custom forms visually and export their schema in seconds with our interactive Form Builder — a powerful no-code tool for building responsive forms through an intuitive drag-and-drop interface.