Blazor Simple Arithmetic Example - blazor-client-side

I made an example to learn Blazor. The example is to multiply the weight and the center of gravity to get the moment.
The goal is to perform calculation when the weight or center of gravity changes.
I get the following error.
The attribute 'onchange' is used two or more times for this element. Attributes must be unique (case-insensitive). The attribute 'onchange' is used by the '#bind' directive attribute.
Can you help me to achieve the goal?
<h3>MomentBasicCalc</h3>
<div class="container">
<div class="row">
<div class="col-sm">
<label for="weight">Weights</label>
<input type="text" class="form-control" id="weight"
#onchange="Calc" #bind="#Weight" />
</div>
<div class="col-sm">
<label for="CG">CG</label>
<input type="text" class="form-control" id="CG"
#onchange="Calc" #bind=#Cg />
</div>
<div class="col-sm">
<label for="Moment">Moment</label>
<input type="text" class="form-control" id="Moment" value=#Moment readonly />
</div>
</div>
</div>
#code {
public double Weight { get; set; }
public double Cg { get; set; }
public double Moment { get; set; }
void Calc()
{
Moment = Weight * Cg;
}
}

It's not supported to attach multiple event handlers on a single event.
You should do something like this:
<div class="container">
<div class="row">
<div class="col-sm">
<label for="weight">Weights</label>
<input type="text" class="form-control" id="weight"
**value="#Weight**
#onchange="SetWeight" "/>
</div>
<div class="col-sm">
<label for="CG">CG</label>
<input type="text" class="form-control" id="CG"
**value=#Cg**
#onchange="SetCG" />
</div>
<div class="col-sm">
<label for="Moment">Moment</label>
<input type="text" class="form-control" id="Moment" value=#Moment readonly />
</div>
</div>
</div>
#code
{
void SetWeight(UIChangeEventArgs e)
{
Weight = (string) e.Value;
Calc();
}
void SetCG(UIChangeEventArgs e)
{
CG = (string) e.Value;
Calc();
}
}

Related

How to move data from one action to another action in the same controller for forms?

How do I move data from an Iactionresult to another action result? I have been trying to display the data from the form and view it another Iactionresult? I attempt to use Tempdata but it seems like there is an error. Could anyone help me with it?
This action displays an individual product details when I click on an particular Id.
[HttpGet]
public IActionResult Details(int id)
{
string sql = String.Format(#"SELECT * FROM WBProduct
WHERE Id = {0}", id);
List<Product> lstProduct = DBUtl.GetList<Product>(sql);
if (lstProduct.Count == 0)
{
TempData["Message"] = $"Product #{id} not found";
TempData["MsgType"] = "warning";
return RedirectToAction("Index");
}
else
{
Product cdd = lstProduct[0];
return View(cdd);
}
}
I would like to display the the details of the product in this IActionResult
[HttpPost]
public IActionResult Create()
{
return View("Create");
}
View for Details:
#model Product
<div>
<div class="form-group row">
<div class="offset-sm-2"><h2>#Model.ProductName</h2></div>
</div>
<div class="form-group row">
<div class="offset-sm-2 col-sm-5">
<img id="ImgPhoto" src="~/images/product/#Model.ProductImage" style="width:400px;" />
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="City">Weight: </label>
<div class="col-sm-5">
<input type="text" asp-for="ProductWeight" class="form-control" readonly />
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="Date">Stock :</label>
<div class="col-sm-5">
<input type="text" asp-for="ProductStock" class="form-control" readonly />
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="Cost">Price: </label>
<div class="col-sm-5">
<input type="text" asp-for="ProductPrice" asp-format="{0:C}" class="form-control" readonly />
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="Story">Description: </label>
<div class="col-sm-5">
<textarea asp-for="ProductDescription" rows="8" cols="20" class="form-control" readonly></textarea>
</div>
</div>
<div class="form-group row">
<a href="http://localhost:50528/Product/Create" class="btn btn-info" role="button" > Add to Cart </a>
</div>
</div>
Create View:
#model Product
<div class="form-group row">
<div class="offset-sm-2"><h2>#Model.ProductName</h2></div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="City">Weight: </label>
<div class="col-sm-5">
<input type="text" asp-for="ProductWeight" class="form-control" readonly />
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="Date">Stock :</label>
<div class="col-sm-5">
<input type="text" asp-for="ProductStock" class="form-control" readonly />
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="Cost">Price: </label>
<div class="col-sm-5">
<input type="text" asp-for="ProductPrice" asp-format="{0:C}" class="form-control" readonly />
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="Story">Description: </label>
<div class="col-sm-5">
<textarea asp-for="ProductDescription" rows="8" cols="20" class="form-control" readonly></textarea>
</div>
</div>
The error message that I got was:
This should be a GET action, not a POST one, then you should extract the info from the TempData and pass it as parameter to the view cshtml.
TempData["Product"] = JsonConvert.SerializeObject(lstProduct[0]);
return RedirectToAction("Create");
Now you can deserialize in the Create action and retrieve your Product
[HttpGet]
public IActionResult Create()
{
// If the caller has prepared a product we can show it.
if(TempData.ContainsKey("Product"))
{
Product p = JsonConvert.DeserializeObject<Product>(TempData["Product"]);
return View(p);
}
else
return View();
}
If you want to move data from one action to another action in the same controller
just call one action from another and put data as an input parameter of another action.
To send message to Index action, at first create a class for the message:
public class ErrorMsg
{
public string Message {get; set;}
public string MessageType {get; set;}
}
Change your action Index to this:
public IActionResult Index(ErrorMsg errorMsg)
{
// if action called from another controller action, details for exapmple,
//errorMsg will contain data from that action
// otherwise errMsg will be an empty default object with empty strings
//Check if error
if(!string.IsNullOrEmpty(errorMsg.Message) ...your error code
else ....your index code here
}
Change your action details code:
public IActionResult Details(int id)
{
string sql = String.Format(#"SELECT * FROM WBProduct
WHERE Id = {0}", id);
List<Product> lstProduct = DBUtl.GetList<Product>(sql);
if (lstProduct.Count == 0)
{
var errMsg = new ErrMessage {
Message = $"Product #{id} not found",
MessageType = "warning"
}
return Index(errMsg);
}
else
{
Product cdd= lstProduct.FirstOrDefault();
//Or you can try again var cdd = lstProduct[0]; if you like it more
return View("Details", cdd);
}
}
Change your create action to this:
public IActionResult Create(Product product)
{
// if action called from another controller action, "product" will contain data //from that action
// otherwise "product" will be posted from the view or it will be an empty model with the default value fields
if(product.Id ==0) ... call add ef code
else ... call update ef code
}
And you have to add <form tag to all your views, othewise if will not post back any data, and add Product.Id hidden field inside of form:
#model Product
#using (Html.BeginForm("Create", "Product", FormMethod.Post)
{
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div>
<input type="hidden" asp-for="#Model.Id" />
<div class="form-group row">
<div class="offset-sm-2"><h2>#Model.ProductName</h2></div>
</div>
<div class="form-group row">
<div class="offset-sm-2 col-sm-5">
<img id="ImgPhoto" src="~/images/product/#Model.ProductImage" style="width:400px;" />
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="City">Weight: </label>
<div class="col-sm-5">
<input type="text" asp-for="ProductWeight" class="form-control" readonly />
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="ProductStock">Stock :</label>
<div class="col-sm-5">
<input type="text" asp-for="ProductStock" class="form-control" readonly />
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="ProductPrice">Price: </label>
<div class="col-sm-5">
<input type="text" asp-for="ProductPrice" asp-format="{0:C}" class="form-control" readonly />
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="ProductDescription">Description: </label>
<div class="col-sm-5">
<textarea asp-for="ProductDescription" rows="8" cols="20" class="form-control" readonly></textarea>
</div>
</div>
<div class="form-group row">
<button class="btn btn-info btn-link" type="submit"> Add to Cart </button>
</div>
</div>
}

How can I create registration page without refreshing in Asp.Net MVC with Razor?

I need to get advice about in Registration Page in Asp.net MVC5 and razor page.
what is the best way for it:
My scenario is I have a registration process in three-step :
In the First Step, the client enters a username and captcha.
In the Second Phase client Enter the Phone number and send SMS by Identity.
If the Entered validation code was code the confirmation message and welcome page appears.
An easy way is we can make a three action result by the separate razor view.
But the problem is I need to do the noted process without any page refreshing.
what is the best way to do it?
Thanks a lot.
I would solve your task with following steps
Download jquery.unobtrusive-ajax.js from here and add a link in your layout.cshtml
create a Components folder in your pages folder
create a Register folder in your Components folder
add a file for your dto
RegisterDto.cs
public class RegisterDto
{
public string Email { get; set; }
public string FirstName { get; set; }
public string SurName { get; set; }
public string Phone { get; set; }
public string Validation { get; set; }
}
create your first form.
I created a new form in the index.cshtml. Here you can see, that I'm using ajax for handling the form. Use a container for replacing with your viewcomponents. In my example, my DOM Object is "content".
index.cshtml
<div id="content">
<div class="col-6">
<form method="post" data-ajax="true" data-ajax-method="post" asp-page="/Index" asp-page-handler="SubPage1" data-ajax-update="#content">
<div class="form-group">
<label asp-for="RegisterDto.Email"></label>
<input asp-for="RegisterDto.Email" class="form-control" />
</div>
<div class="form-group">
<label asp-for="RegisterDto.FirstName"></label>
<input asp-for="RegisterDto.FirstName" class="form-control" />
</div>
<div class="form-group">
<label asp-for="RegisterDto.SurName"></label>
<input asp-for="RegisterDto.SurName" class="form-control" />
</div>
<button class="btn btn-success" type="submit">next</button>
</form>
</div>
Create a new ViewComponentClass. Over the page-parameter you can switch between pages, and the registerDto is your model you can pass between pages.
RegisterViewComponent.cs
public class RegisterViewComponent : ViewComponent
{
public async Task<IViewComponentResult> InvokeAsync(string Page, RegisterDto registerDto)
{
return View(Page, registerDto);
}
}
create your views in your viewcomponent.
SubPage1.cshtml
<div class="col-6">
<form method="post" data-ajax="true" data-ajax-method="post" asp-page-handler="SubPage2" data-ajax-update="#content">
<div class="form-group">
<label asp-for="Email"></label>
<input asp-for="Email" class="form-control disabled" />
</div>
<div class="form-group">
<label asp-for="FirstName"></label>
<input asp-for="FirstName" class="form-control disabled" />
</div>
<div class="form-group">
<label asp-for="SurName"></label>
<input asp-for="SurName" class="form-control disabled" />
</div>
<div class="form-group">
<label asp-for="Phone"></label>
<input asp-for="Phone" class="form-control" />
</div>
<button class="btn btn-success" type="submit">next</button>
</form>
SubPage2.cshtml
<div class="col-6">
<form method="post" data-ajax="true" data-ajax-method="post" asp-page-handler="SubPage3" data-ajax-update="#content">
<div class="form-group">
<label asp-for="Email"></label>
<input asp-for="Email" class="form-control disabled" />
</div>
<div class="form-group">
<label asp-for="FirstName"></label>
<input asp-for="FirstName" class="form-control disabled" />
</div>
<div class="form-group">
<label asp-for="SurName"></label>
<input asp-for="SurName" class="form-control disabled" />
</div>
<div class="form-group">
<label asp-for="Phone"></label>
<input asp-for="Phone" class="form-control disabled" />
</div>
<div class="form-group">
<label asp-for="Validation"></label>
<input asp-for="Validation" class="form-control" />
</div>
<button class="btn btn-success" type="submit">Done</button>
</form>
</div>
default.cshtml
<h1>done</h1>
done!

Form Validation not working in Blazor 3.1

I am using EF to save data in DB and so far farm works fine and saves the data, but when i try to add validation to form it doesnt work & doesnt show any error message or save any data in database.
Example of working & non working code.
Below code without validation
Employee.cs
using System.ComponentModel.DataAnnotations;
namespace BlazorSPA1.Data
{
public class Employee
{
[MaxLength(50)]
public string Id { get; set; }
[MaxLength(50)]
public string Name { get; set; }
[MaxLength(50)]
public string Department { get; set; }
[MaxLength(100)]
public string Designation { get; set; }
[MaxLength(100)]
public string Company { get; set; }
[MaxLength(100)]
public string City { get; set; }
}
}
AddEmployee.razor
#page "/addemployee"
#inject NavigationManager NavigationManager
#inject IEmployeeService EmployeeService
<h2>Create Employee</h2>
<hr />
<form>
<div class="row">
<div class="col-md-8">
<div class="form-group">
<label for="Name" class="control-label">Name</label>
<input for="Name" class="form-control" #bind="#employee.Name" />
</div>
<div class="form-group">
<label for="Department" class="control-label">Department</label>
<input for="Department" class="form-control" #bind="#employee.Department" />
</div>
<div class="form-group">
<label for="Designation" class="control-label">Designation</label>
<input for="Designation" class="form-control" #bind="#employee.Designation" />
</div>
<div class="form-group">
<label for="Company" class="control-label">Company</label>
<input for="Company" class="form-control" #bind="#employee.Company" />
</div>
<div class="form-group">
<label for="City" class="control-label">City</label>
<input for="City" class="form-control" #bind="#employee.City" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<input type="button" class="btn btn-primary" #onclick="#CreateEmployee" value="Save" />
<input type="button" class="btn" #onclick="#Cancel" value="Cancel" />
</div>
</div>
</div>
</form>
#code {
Employee employee = new Employee();
protected async Task CreateEmployee()
{
await EmployeeService.CreateEmployee(employee);
NavigationManager.NavigateTo("listemployees");
}
void Cancel()
{
NavigationManager.NavigateTo("listemployees");
}
}
Code which is not working after i made validation changes
Employee.cs
using System.ComponentModel.DataAnnotations;
namespace BlazorSPA1.Data
{
public class Employee
{
[MaxLength(50)]
public string Id { get; set; }
[Required]
[StringLength(20)]
public string Name { get; set; }
[Required]
[StringLength(20)]
public string Department { get; set; }
[MaxLength(100)]
public string Designation { get; set; }
[MaxLength(100)]
public string Company { get; set; }
[MaxLength(100)]
public string City { get; set; }
}
}
AddEmployeeValidation.razor
#page "/addemployeeValidation"
#inject NavigationManager NavigationManager
#inject IEmployeeService EmployeeService
<h2>Create Employee</h2>
<hr />
<EditForm Model="#employee" OnValidSubmit="#CreateEmployee">
<DataAnnotationsValidator />
<div class="row">
<div class="col-md-8">
<div class="form-group">
<label for="Name" class="control-label">Name</label>
<input for="Name" class="form-control" #bind="#employee.Name" />
<ValidationMessage For="#(()=> employee.Name)" />
</div>
<div class="form-group">
<label for="Department" class="control-label">Department</label>
<input for="Department" class="form-control" #bind="#employee.Department" />
</div>
<div class="form-group">
<label for="Designation" class="control-label">Designation</label>
<input for="Designation" class="form-control" #bind="#employee.Designation" />
</div>
<div class="form-group">
<label for="Company" class="control-label">Company</label>
<input for="Company" class="form-control" #bind="#employee.Company" />
</div>
<div class="form-group">
<label for="City" class="control-label">City</label>
<input for="City" class="form-control" #bind="#employee.City" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<input type="button" class="btn btn-primary" value="Save" />
<input type="button" class="btn" #onclick="#Cancel" value="Cancel" />
</div>
</div>
</div>
</EditForm>
#code {
Employee employee = new Employee();
protected async Task CreateEmployee()
{
await EmployeeService.CreateEmployee(employee);
NavigationManager.NavigateTo("listemployees");
}
void Cancel()
{
NavigationManager.NavigateTo("listemployees");
}
}
I am using below code example show in this example https://www.c-sharpcorner.com/article/visual-studio-extension-for-blazor-spa-with-ef-core-3-1/
When i add validation code, it open Add Employee page but nothing happens no validation message no form submit even no data is save in database. not sure where issue is
I had made a tiny mistake which went un-noticed, Validation started working when i changed the input type to submit
<input type="button" class="btn btn-primary" value="Save" />
Correct
<input type="submit" class="btn btn-primary" value="Save" />

Class receiving the value reset

I'm doing insert via post, but my class is getting the zero values of the inputs.
The values of the inputs are passed via variable and corrections are displayed, but at the time of the post are coming down.
The most interesting thing is if you type inside the input, then the values come correctly.
<form method="post">
<div class="col-sm-3">
<label>SALDO</label>
<div style="border:1px solid #bbb9b9; border-radius:3px;"></div>
<br />
<div class="form-group">
<label asp-for="Caixas.ValorFinalDinheiro" class="control-label"></label>
<input asp-for="Caixas.ValorFinalDinheiro" name="Caixas.ValorFinalDinheiro" id="Caixas.ValorFinalDinheiro" class="form-control finalFundo" disabled="disabled" />
</div>
<div class="form-group">
<label asp-for="Caixas.ValorFinalCheque" class="control-label"></label>
<input asp-for="Caixas.ValorFinalCheque" class="form-control finalFundo" disabled="disabled"/>
</div>
<div class="form-group">
<label asp-for="Caixas.ValorFinalBoleto" class="control-label"></label>
<input asp-for="Caixas.ValorFinalBoleto" class="form-control finalFundo" disabled="disabled" />
</div>
<div class="form-group">
<label asp-for="Caixas.ValorFinalCartao" class="control-label"></label>
<input asp-for="Caixas.ValorFinalCartao" class="form-control finalFundo" disabled="disabled" />
</div>
<div class="form-group">
<label asp-for="Caixas.ValorFinalDeposito" class="control-label"></label>
<input asp-for="Caixas.ValorFinalDeposito" class="form-control finalFundo" disabled="disabled" />
</div>
<div class="form-group">
<label asp-for="Caixas.ValorFinal" class="control-label"></label>
<input asp-for="Caixas.ValorFinal" class="form-control" style="background-color:#9FF781" />
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<input type="submit" value="Confirmar o Fechamento do Caixa" class="btn btn-primary btn-sm" />
<a asp-page="Index" class="btn btn-success btn-sm">Retorna ao Caixa</a>
</div>
</div>
</form>
[BindProperty]
public Caixas Caixas { get; set; }
public async Task<IActionResult> OnPostAsync()
{
var C = _context.Caixas.Find(Caixas.Id);
C.fechado = true;
C.DataFinal = DateTime.Now;
C.HoraFinal = DateTime.Now;
C.FuncionarioFechamentoId = _userManager.GetUserId(HttpContext.User);
C.ValorFinalDinheiro = Caixas.ValorFinalDinheiro;
C.ValorFinalCheque = Caixas.ValorFinalCheque;
C.ValorFinalBoleto = Caixas.ValorFinalBoleto;
C.ValorFinalCartao = Caixas.ValorFinalCartao;
C.ValorFinalDeposito = Caixas.ValorFinalDeposito;
C.ValorFinal = Caixas.ValorFinal;
C.ValorSaida = Caixas.ValorSaida;
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
Remove Caixas property and add it as parameter in your OnPostAsync method:
// [BindProperty]
// public Caixas Caixas { get; set; }
[HttpPost]
public async Task<IActionResult> OnPostAsync([FromBody]Caixas c)
{
// ...
}

Thymeleaf doesn't use formatters for inputs using data-th-field

I found a problem using Thymeleaf in a Spring Boot application.
Versions:
Spring Boot 1.3.4 and 1.3.3
My Entity:
#Entity
public class MyEntity {
#Id
#GeneratedValue
private Long id;
#Version
private int version;
#DateTimeFormat(pattern="dd/MM/yyyy")
private Calendar calendar;
#DateTimeFormat(pattern="dd/MM/yy")
private Date date;
#NumberFormat(pattern="#0.00000")
private Double aDouble;
}
My Controller:
#RequestMapping(value = "/{myEntity}/edit-form",
method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
public String editForm(#PathVariable MyEntity myEntity, Model model) {
return "myEntity/edit";
}
My myEntity/edit.html template:
<form class="form-horizontal" method="POST"
data-th-object="${myEntity}"
data-th-action="#{/myEntity/{id}(id=*{id})}">
<input type="hidden" name="_method" value="PUT" />
<div class="form-group"
data-th-classappend="${#fields.hasErrors('calendar')}? 'has-error has-feedback'">
<label for="calendar" class="col-md-3 control-label">Calendar</label>
<div class="col-md-3">
<input type="text" class="form-control"
data-th-field="*{calendar}"/>
<span data-th-text="*{{calendar}}"></span>
</div>
</div>
<div class="form-group"
data-th-classappend="${#fields.hasErrors('date')}? 'has-error has-feedback'">
<label for="date" class="col-md-3 control-label">Date</label>
<div class="col-md-3">
<input type="text" class="form-control"
data-th-field="*{date}"/>
<span data-th-text="*{{date}}"></span>
</div>
</div>
<div class="form-group"
data-th-classappend="${#fields.hasErrors('aDouble')}? 'has-error has-feedback'">
<label for="date" class="col-md-3 control-label">aDouble</label>
<div class="col-md-3">
<input type="text" class="form-control"
data-th-field="*{{aDouble}}"/>
<span data-th-text="*{{aDouble}}"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<div class="pull-right">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</div>
</div>
</form>
When I try to show this page I get:
<body>
<form class="form-horizontal" method="POST" action="/myEntity/1">
<input type="hidden" name="_method" value="PUT" />
<div class="form-group">
<label for="calendar" class="col-md-3 control-label">Calendar</label>
<div class="col-md-3">
<input type="text" class="form-control" id="calendar" name="calendar"
value="java.util.GregorianCalendar[time=1451602800000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/Madrid",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=165,lastRule=java.util.SimpleTimeZone[id=Europe/Madrid,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2016,MONTH=0,WEEK_OF_YEAR=53,WEEK_OF_MONTH=0,DAY_OF_MONTH=1,DAY_OF_YEAR=1,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=3600000,DST_OFFSET=0]" />
<span>01/01/2016</span>
</div>
</div>
<div class="form-group">
<label for="date" class="col-md-3 control-label">Date</label>
<div class="col-md-3">
<input type="text" class="form-control" id="date" name="date"
value="2016-02-01 00:00:00.0" />
<span>01/02/16</span>
</div>
</div>
<div class="form-group">
<label for="date" class="col-md-3 control-label">aDouble</label>
<div class="col-md-3">
<input type="text" class="form-control" id="aDouble" name="aDouble"
value="0.1" />
<span>0.10000</span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<div class="pull-right">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</div>
</div>
</form>
</body>
As you can see, the values of all fields aren't formatted as expected (see span which uses the same value through th-text attribute) using toString() method for all values.
Anybody can help me? Thank's in advance.
EDIT 1: I've create a new issue about it
Solved. The problem was in the requestMapping definition:
#RequestMapping(value = "/{myEntity}/edit-form", method = RequestMethod.GET,
produces = MediaType.TEXT_HTML_VALUE)
public String editForm(#PathVariable MyEntity myEntity, Model model) {
}
It requires a BindingResult in request context to get PropertyEditor which can transform object value to String. So, including BindingResult in request mapping definition all works as expected:
#RequestMapping(value = "/{myEntity}/edit-form", method = RequestMethod.GET,
produces = MediaType.TEXT_HTML_VALUE)
public String editForm(#ModelAttribute MyEntity myEntity, BindingResult result,
Model model) {
}
Note that object must be annotated with #ModelAttribute and BindingResult must be declared just after #ModelAttribute.
Thank you to everyone who tried to help me.
For date I'd use:
${#dates.format(date, 'dd/MMM/yyyy HH:mm')}
and for numbers:
${#numbers.formatDecimal(num,3,2,'COMMA')}
In documentation it's explained for numbers and dates.

Resources