Carfamsoft.ModelToView
1.1.0
See the version list below for details.
dotnet add package Carfamsoft.ModelToView --version 1.1.0
NuGet\Install-Package Carfamsoft.ModelToView -Version 1.1.0
<PackageReference Include="Carfamsoft.ModelToView" Version="1.1.0" />
paket add Carfamsoft.ModelToView --version 1.1.0
#r "nuget: Carfamsoft.ModelToView, 1.1.0"
// Install Carfamsoft.ModelToView as a Cake Addin #addin nuget:?package=Carfamsoft.ModelToView&version=1.1.0 // Install Carfamsoft.ModelToView as a Cake Tool #tool nuget:?package=Carfamsoft.ModelToView&version=1.1.0
Updating your projects referencing versions 1.0.x to v1.1.0
Namespace changes:
// Before:
using Carfamsoft.ModelToView.ComponentModel.DataAnnotations;
// After:
using Carfamsoft.ModelToView.ViewAnnotations;
Custom attributes changes
// Before:
[DisplayResource(ResourceType = typeof(DisplayStrings))]
public class RegisterBusinessModel
{
[Required]
[Display(Name = "UserName", GroupName = "Authentication", Order = 1)]
[DisplayHint(Icon = "fas fa fa-user")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6, ErrorMessageResourceName = "PasswordLengthError", ErrorMessageResourceType = typeof(DisplayStrings))]
[DataType(DataType.Password)]
[Display(Name = "Password", GroupName = "Authentication", Order = 2)]
[DisplayHint(Icon = "fas fa fa-lock")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "ConfirmPassword", GroupName = "Authentication", Order = 3)]
[DisplayHint(Icon = "fas fa fa-lock")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.", ErrorMessageResourceName = "ConfirmPasswordError", ErrorMessageResourceType = typeof(DisplayStrings))]
public string ConfirmPassword { get; set; }
}
// After:
[FormDisplayDefault(ResourceType = typeof(DisplayStrings))]
public class RegisterBusinessModel
{
[Required]
[Display(Name = "UserName", GroupName = "Authentication", Order = 1)]
[FormDisplay(Icon = "fas fa fa-user")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6, ErrorMessageResourceName = "PasswordLengthError", ErrorMessageResourceType = typeof(DisplayStrings))]
[DataType(DataType.Password)]
[Display(Name = "Password", GroupName = "Authentication", Order = 2)]
[FormDisplay(Icon = "fas fa fa-lock")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "ConfirmPassword", GroupName = "Authentication", Order = 3)]
[FormDisplay(Icon = "fas fa fa-lock")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.", ErrorMessageResourceName = "ConfirmPasswordError", ErrorMessageResourceType = typeof(DisplayStrings))]
public string ConfirmPassword { get; set; }
}
Notable changes are around two custom attributes:
- Carfamsoft.ModelToView.ComponentModel.DataAnnotations.DisplayResourceAttribute
- Replaced by Carfamsoft.ModelToView.ViewAnnotations.FormDisplayDefaultAttribute.
- Carfamsoft.ModelToView.ComponentModel.DataAnnotations.DisplayHintAttribute
- Replaced by Carfamsoft.ModelToView.ViewAnnotations.FormDisplayAttribute
Sample view model annotations
using Carfamsoft.ModelToView.Testing.Resources;
using Carfamsoft.ModelToView.ViewAnnotations;
using System;
using System.ComponentModel.DataAnnotations;
namespace Carfamsoft.ModelToView.Testing
{
[FormDisplayDefault(ShowGroupName = true, ResourceType = typeof(DisplayStrings))]
public class UpdateUserModel
{
[Required]
[StringLength(100)]
[FormDisplay(GroupName = "PersonalInfo", Icon = "fas fa-user")]
public string FirstName { get; set; }
[Required]
[StringLength(100)]
[FormDisplay(GroupName = "PersonalInfo", Icon = "fas fa-user")]
public string LastName { get; set; }
[Required]
[StringLength(255)]
[EmailAddress]
[FormDisplay(GroupName = "ContactDetails", Icon = "fas fa-envelope", UITypeHint = "email")]
public string Email { get; set; }
[StringLength(30)]
[FormDisplay(GroupName = "ContactDetails", Icon = "fas fa-phone", UITypeHint = "phone")]
public string PhoneNumber { get; set; }
}
public class AutoUpdateUserModel : UpdateUserModel
{
[DisplayIgnore]
public string Id { get; set; }
[Range(typeof(DayOfWeek), "Monday", "Friday")]
[FormDisplay(GroupName = "PleaseSelect", Tag = "select", Name = "", Order = 1, Prompt = nameof(FavouriteWorkingDay), Icon = "fas fa-calendar")]
public string FavouriteWorkingDay { get; set; }
[FormDisplay(GroupName = "PleaseSelect", UIHint = "select", Name = "", Order = 2)]
public int AgeRange { get; set; }
//[Range(typeof(ConsoleColor), "Black", "White")]
[FormDisplay(Type = "radio", Order = 3, Options = "Black|Blue|White")]
public string FavouriteColor { get; set; }
[FormDisplay(Order = 4, Description = "LoginWithEmailAndSms")]
public bool TwoFactorEnabled { get; set; }
}
}
Sample unit test
using Carfamsoft.ModelToView.Mvc;
using Carfamsoft.ModelToView.Shared;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
using System.Globalization;
using static System.Threading.Thread;
namespace Carfamsoft.ModelToView.Testing.Tests
{
[TestClass]
public class NestedTagBuilderTest
{
[TestMethod]
public void Should_Render_AutoEditForm()
{
// arrange
CurrentThread.CurrentUICulture = new CultureInfo("fr");
var model = new AutoUpdateUserModel
{
FirstName = "Abdoul",
LastName = "Kaba",
Email = "abdoul.kaba@example.com",
PhoneNumber = "92469522",
};
var formAction = "https://example.com/register";
var form = new FormAttributes
{
Id = "registerUserForm",
Action = formAction,
//OptionsGetter = null,
//DisabledGetter = null,
};
var renderOptions = new ControlRenderOptions
{
CamelCaseId = true,
GenerateIdAttribute = true,
GenerateNameAttribute = true,
OptionsGetter = propertyName =>
{
if (propertyName.Equals(nameof(AutoUpdateUserModel.AgeRange)))
{
return new []
{
new SelectOption(id: 0, value: "[Your most appropriate age]", isPrompt: true),
new SelectOption(1, "Minor (< 18)"),
new SelectOption(2, "Below or 25"),
new SelectOption(3, "Below or 30"),
new SelectOption(4, "Below or 40"),
new SelectOption(5, "Below 50"),
new SelectOption(6, "Between 50 and 54"),
new SelectOption(7, "Between 55 and 60"),
new SelectOption(8, "Above 60"),
new SelectOption(9, "Above 70"),
new SelectOption(10, "Above 80"),
};
}
return null;
}
};
// act
var result = form.RenderAutoEditForm(model, renderOptions).RenderButton(text: " Save", config: button =>
{
button.AddChild(NestedTagBuilder.Create("i").AddClass("fas fa fa-save"));
}).ToString();
Debug.WriteLine(result);
// assert
Assert.IsTrue(true == result?.Contains($"action=\"{formAction}\""));
Assert.IsTrue(result.Contains("abdoul.kaba@example.com"));
}
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
- Carfamsoft.ModelToView.Shared (>= 1.1.0)
- Carfamsoft.ModelToView.ViewAnnotations (>= 1.1.0)
- Microsoft.AspNet.WebPages (>= 3.2.7)
- System.ComponentModel.Annotations (>= 5.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Carfamsoft.ModelToView:
Package | Downloads |
---|---|
Carfamsoft.ModelToView.Mvc
Provides extension methods for the HtmlHelper class in order to render auto-generated, editable forms and HTML controls. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Obsolete:
Many classes that supported version 1.0.x were deprecated due to incomplete implementation of rendering
processes. These changes are non breaking but might display a lot of deprecation warnings when building
existing projects where these classes were in directl use.
All deprecated classes are in the Carfamsoft.ModelToView.WebPages namespace. Namely, these are:
- ControlInfo
- ControlInfoCollection
- FormControlsRenderer (replaced by AutoInputBase)
- IControlRenderer
- ObjectHtmlViewEngine
Several supporting NuGet packages have been released which improve and facilitate clean code usage:
- Carfamsoft.ModelToView.Shared
- Carfamsoft.ModelToView.ViewAnnotations