I am developing a register where I will have a table
with some additional details that must be inserted together with the main register.
But one of the columns of this table
is calculated, which is where I'm having difficulty performing this calculation.
Basically, I need to calculate the ValorCalculado
field based on Percentual
applied to ValorPrincipal
And this value should appear as the user enters the MainValue field or Percentage
Yes, I did a very simple example, think of an input system of products in stock, where the user has read an XML and this note has at least 10 products, and is launching the percentage of product profit per product and wanting to see what the sales value as you change these items. Imagine having to be making a request to the server every time it has an altered percentage in the View, so it would be good to do the calculation both in View and not only in the server.
In summary, I need the CalculatedValue to display the PrimaryValue * Percent every time I have a change in PrimaryValue or Percentage
Follow the example code
public class CadastroController : Controller
{
// GET: Cadastro
public ActionResult Index()
{
CadastroViewModel model = new CadastroViewModel();
model.Detalhes.Add(new CadastroDetalhesViewModel());
model.Detalhes.Add(new CadastroDetalhesViewModel());
model.Detalhes.Add(new CadastroDetalhesViewModel());
model.Detalhes.Add(new CadastroDetalhesViewModel());
return View(model);
}
[HttpPost]
public ActionResult Index(CadastroViewModel model)
{
return View(model);
}
}
Model
public class CadastroViewModel
{
public CadastroViewModel()
{
Detalhes = new List<CadastroDetalhesViewModel>();
}
public int CadastroId { get; set; }
public DateTime DataInicio { get; set; }
public DateTime DataFim { get; set; }
public decimal ValorPrincipal { get; set; }
public decimal Percentual { get; set; }
public decimal ValorCalculado { get; set; }
public List<CadastroDetalhesViewModel> Detalhes { get; set; }
}
public class CadastroDetalhesViewModel
{
public int CadastroId { get; set; }
public decimal ValorPrincipal { get; set; }
public decimal Percentual { get; set; }
public decimal ValorCalculado { get; set; }
}
View
@model AspNetMvcCalcularValoresTabela.Models.CadastroViewModel
@{
ViewBag.Title = "View";
}
<h2>View</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>CadastroViewModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.CadastroId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CadastroId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CadastroId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DataInicio, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DataInicio, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DataInicio, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DataFim, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DataFim, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DataFim, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ValorPrincipal, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ValorPrincipal, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ValorPrincipal, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Percentual, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Percentual, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Percentual, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ValorCalculado, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ValorCalculado, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ValorCalculado, "", new { @class = "text-danger" })
</div>
</div>
<table>
<thead>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.Detalhes.Count; i++)
{
<tr>
<td>@Html.EditorFor(a => a.Detalhes[i].ValorPrincipal, new { htmlAttributes = new { @class = "form-control" } })</td>
<td>@Html.EditorFor(a => a.Detalhes[i].Percentual, new { htmlAttributes = new { @class = "form-control" } })</td>
<td>@Html.EditorFor(a => a.Detalhes[i].ValorCalculado, new { htmlAttributes = new { @class = "form-control" } })</td>
</tr>
}
</tbody>
</table>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>