2
I’m developing a record where I’ll have a table
with some additional details that should be entered together with the main register.
But one of the columns of this table
is calculated, which is where I’m having trouble making this calculation.
Basically, I need to calculate the field ValorCalculado
on the basis of Percentual
applied on the ValorPrincipal
And this value should appear as the user enters the Main Value or Percentage field
Yes, I made a very simple example, think about an entry system of products in stock, where the user has read an XML and this note has at least 10 products, and is releasing the percentage of product profit per product and wanting to see what the sale value is as it changes these items. Imagine having to make a request to the server every time you have a percentage changed in the View, so it would be good to make the calculation be in the View as well and not only in the server.
In short, I need the Calculated Value to present the Main Value*Percentage every time I have a change in the Main Value 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>
Yes, I made a very simple example, think about a system of entry of products in stock, where the user is launching the percentage of profit of product by product and want to see what the sale value is as you change these items. Imagine having to make a request to the server every time you have a changed percentage in the View, so that’s why I need to calculate it in the View as well and not just on the server.
– Pablo Tondolo de Vargas
@Pablovargas But if the user will enter the calculated value, it makes no sense to have to calculate it, unless it is validations. Now, if this calculation will be generated automatically in the view just to assist, ok. However, if you need this precise value, the ideal is to make a request to the server for each change yes, even in the view, by ajax or jQuery.
– Randrade
@Pablovargas If you care about security, you should not leave any information in the view that cannot be changed. But this goes into other factors. Let me know if the answer doesn’t help you
– Randrade
but the
ValorCalculado
will not be typed by the user, will be an aid field of the calculation of the main value*Percentage.– Pablo Tondolo de Vargas
@Pablovargas But being in
View
it is amenable to change. But as you said, you made a simple example, so I can’t state anything about your code.– Randrade
In short, I need the Calculated Value to present the Main Value*Percentage every time I have a change in the Main Value or Percentage
– Pablo Tondolo de Vargas
@Pablovargas Well, I did it both ways, just test and tell me if it helps you.
– Randrade
does not help, because your example would have to come with the values already informed from the server to the view, what I want is that as the values are being typed, the value is being calculated
– Pablo Tondolo de Vargas
@Pablovargas So you need something with javascript or jQuery, which is not specified in the question. If you don’t have the answer you’re looking for, tomorrow I’ll change the way you need it.
– Randrade
Blz, I’ll be waiting
– Pablo Tondolo de Vargas
@Pablovargas, see if he’ll see you now.
– Randrade
Let’s go continue this discussion in chat.
– Pablo Tondolo de Vargas