ObjToForm 1.1.0

dotnet add package ObjToForm --version 1.1.0
                    
NuGet\Install-Package ObjToForm -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ObjToForm" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ObjToForm" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="ObjToForm" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ObjToForm --version 1.1.0
                    
#r "nuget: ObjToForm, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ObjToForm@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ObjToForm&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=ObjToForm&version=1.1.0
                    
Install as a Cake Tool

ObjToForm · License: MIT .NET C#

ObjToForm is a .NET tool for building HTML forms from objects meant to accelerate testing without spending time writing HTML Code.

Quick start

Several quick start options are available:

  • Install with NuGet: Install-Package ObjToForm
  • Install with .NET CLI: dotnet add package ObjToForm
  • Clone the repo: git clone https://github.com/Adler-Targino/ObjToForm.git

Demo

Example object


    public class DemoObj
    {
        public string PublicString { get; set; }
        public int PublicInt { get; set; }
        public DateTime PublicDateTime { get; set; }
        public float PublicFloat { get; set; }
        public bool PublicBool { get; set; }
        private string IShouldntBeHere {get; set;}
        public DemoOptions PublicOptionsEnum { get; set; }
    }

    public enum DemoOptions
    {
        Option1,
        Option2,
        Option3,
        Option4,
        Option5
    }

After installing the library you can convert C# objects directly into HTML forms

Razor Page Example


@using ObjToForm;

...


<form method="post">
    @ObjToForm.ConvertToRawHtmlForm(typeof(DemoObj))
    
    @ObjToForm.ConvertToRawHtmlForm(typeof(DemoObj), "Obj", true)
</form>



<hr>


<form method="post">
    @ObjToForm.ConvertToBootstrapForm(typeof(DemoObj))
    
    @ObjToForm.ConvertToBootstrapForm(typeof(DemoObj), "Obj", true)
</form>

Result

Plain HTML Form

<img width="349" height="379" alt="image" src="https://github.com/user-attachments/assets/b8d84c81-ed14-4a4f-b8bf-0dfaf7f6b376" />

Bootstrap-styled form

<img width="1367" height="480" alt="image" src="https://github.com/user-attachments/assets/7ea47cdc-ba28-4946-a261-8421e158fcb6" />

Receive the values in your controller as usual

public IActionResult OnPost(string PublicString, int PublicInt, DateTime PublicDateTime, float PublicFloat, bool PublicBool, DemoOptions PublicOptionsEnum)
{
    ...
    return Page();
}

//Or, if you are using ModelBinding
public IActionResult OnPost(DemoObj Obj)
{
    ...
    return Page();
}

Notes

  • As shown in the previous example, only public properties are rendered.
  • Using Bootstrap-styled forms requires to previously add bootstrap to your project

Advanced Usage

ObjToForm has a few more tools that can be helpfull if you intend to create more elaborated forms

Customization with Attributes

Attributes can be applied directly to the class properties, making it possible to define CSS classes, inline styles, labels, or even custom HTML attributes for the rendered elements.

Available Attributes

  • [DivClass("className", bool override = false)]
    Sets the CSS class(es) for the <div> wrapping the field.
    The override parameter defines whether to replace the default classes (in case of using ConvertToBootstrapForm) or just append them.

  • [LabelClass("className", bool override = false)]
    Sets the CSS class(es) for the <label> of the field.

  • [InputClass("className", bool override = false)]
    Sets the CSS class(es) for the <input> or <select> element.

  • [DivStyle("style")]
    Adds inline CSS styles to the <div>.

  • [LabelStyle("style")]
    Adds inline CSS styles to the <label>.

  • [InputStyle("style")]
    Adds inline CSS styles to the <input>.

  • [Label(bool enabled)]
    Hides the label when enabled = false.

  • [Label("text")]
    Sets the text of the label.

  • [HtmlAttribute("attribute=value")]
    Adds custom HTML attributes directly to the field (e.g., maxlength=50, data-custom="123").

Quick Note: Multiple attributes (including attributes of the same type) can be assigned for each property

Example Object

public class CustomDemoObj
{
    [Label("Full Name")]
    [InputClass("form-control mb-2")]
    [DivClass("col-md-6")]
    [HtmlAttribute("placeholder=Enter your name")]
    public string Name { get; set; }

    [Label("Birth Date")]
    [InputClass("form-control")]
    [InputStyle("max-width:200px;")]
    public DateTime BirthDate { get; set; }

    [Label("Email", true)]
    [InputClass("form-control")]
    [HtmlAttribute("type=email")]
    public string Email { get; set; }

    [Label("Notes")]
    [InputClass("form-control")]
    [HtmlAttribute("rows=4")]
    public string Notes { get; set; }
}

Calling the ConvertToRawHtmlForm method while passing this object as a paramter will result in the following result:

<img width="1349" height="447" alt="image" src="https://github.com/user-attachments/assets/16ec6b2a-1c8c-456a-aeaa-accf2e4cd2e0" />

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 283 9/24/2025
1.0.1 188 9/8/2025