I'm using MaskMoney to create a mask in the field for the user to type. wanted to remove the mask, saving only the numbers and the comma (2 decimal places) in the bank.
$ 1,300,000.00
Just saving
1300000.00
I'm using asp.net-mvc, follow the controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "PessoaID,Nome,Telefone,Celular,Whatsapp,Email,CPF,Tipo,RG,CTPS,DataNascimento,DataAdmissao,DataDemissao,Senha,CargoID")] Funcionario funcionario)
{
//salario = Convert.ToString(salario);
salario = Regex.Replace(salario, "[^0-9,]", "");
funcionario.Salario = Convert.ToDecimal(salario);
funcionario.tipo = 1;
if (ModelState.IsValid)
{
db.Pessoas.Add(funcionario);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CargoID = new SelectList(db.Cargos, "CargoID", "Descricao", funcionario.CargoID);
return View(funcionario);
}
Create:
@model SAQ.Models.Funcionario
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4 onmouseover="tooltip.pop(this, 'Este é meu texto na tooltip.')">Funcionario</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Nome, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Nome, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Nome, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Telefone, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Telefone, new { htmlAttributes = new { @class = "form-control telefone" } })
@Html.ValidationMessageFor(model => model.Telefone, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Celular, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Celular, new { htmlAttributes = new { @class = "form-control celular", id = "txt-tel" } })
@Html.ValidationMessageFor(model => model.Celular, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Whatsapp, htmlAttributes: new { @class = "control-label col-md-2", id = "bt-copiar", title = "Clique em mim para copiar o Celular" })
<div class="col-md-10">
@Html.EditorFor(model => model.Whatsapp, new { htmlAttributes = new { @class = "form-control celular", id = "txt-wpp", title = "Clique no Texto WhatsApp ao lado para copiar o Celular" } })
@*<div class="btn" id="bt-copiar">Copiar</div>*@
@Html.ValidationMessageFor(model => model.Whatsapp, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CPF, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CPF, new { htmlAttributes = new { @class = "form-control cpf" } })
@Html.ValidationMessageFor(model => model.CPF, "", new { @class = "text-danger" })
</div>
</div>
@*<div class="form-group">
@Html.LabelFor(model => model.tipo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.tipo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.tipo, "", new { @class = "text-danger" })
</div>
</div>*@
<div class="form-group">
@Html.LabelFor(model => model.RG, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RG, new { htmlAttributes = new { @class = "form-control rg" } })
@Html.ValidationMessageFor(model => model.RG, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CTPS, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CTPS, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CTPS, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DataNascimento, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DataNascimento, new { htmlAttributes = new { @class = "form-control data" } })
@Html.ValidationMessageFor(model => model.DataNascimento, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DataAdmissao, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DataAdmissao, new { htmlAttributes = new { @class = "form-control data" } })
@Html.ValidationMessageFor(model => model.DataAdmissao, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Salario, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Salario, new { htmlAttributes = new { @class = "form-control " } })
@Html.ValidationMessageFor(model => model.Salario, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Senha, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Senha, new { htmlAttributes = new { @class = "form-control", id="Senha"} })
@Html.ValidationMessageFor(model => model.Senha, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CargoID, "Cargo", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CargoID", null, "Selecione", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CargoID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Salvar" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
<script type="text/javascript">
$(document).ready(function () {
$(".telefone").inputmask("mask", { "mask": "(99)9999-9999" });
$(".celular").inputmask("mask", { "mask": "(99)99999-9999" });
$(".cpf").inputmask("mask", { "mask": "999.999.999-99" }, { reverse: true });
$(".data").inputmask("mask", { "mask": "99/99/9999" });
$(".rg").inputmask("mask", { "mask": "99.999.999-9" });
$(".dinheiro").maskMoney({ prefix: 'R$ ', allowNegative: true, thousands: '.', decimal: ',', affixesStay: true });
});
</script>
<script> //copia os caracteres do telefone
$('#bt-copiar').on('click', function () {
$('#txt-wpp').val($('#txt-tel').val());
});
</script>
<script>
$(document).ready(function (e) {
$('#Senha').on('focus', function () {
$(this).attr('type', 'text');
}).on('blur', function () {
$(this).attr('type', 'password');
});
});
</script>
}
class (I'm just putting the attribute that gives me the problem):
[DisplayFormat(DataFormatString = "{0:c}")]
[Required(ErrorMessage = "Preencha o Salário")]
[DisplayName("Salário")]
public decimal Salario { get; set; }