Question: Summing values of two textboxes in Asp.net MVC

Question

Summing values of two textboxes in Asp.net MVC

Answers 1
Added at 2017-01-04 17:01
Tags
Question

Im new to ASP .NET MVC Web Aplications and i please need some help. I need to summ two values ("Cena" and "Kolicina") and save into textbox "Znesek". How should i do this? I tried this (How to multiply values of two textboxes in Asp.net MVC) but its not working for me :/

Here is my .cshtml file:

    <div class="form-group">
        @Html.LabelFor(model => model.kolicina, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.kolicina, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.kolicina, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.cena, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.cena, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.cena, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.znesek, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.znesek, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.znesek, "", new { @class = "text-danger" })
        </div>
    </div>

Acctual form

Please help me, what i need to change or where i need to do some action?

EDIT: Sure, i have @Html.BeginForm and everything what is needed for View code, this is just partial code. Everything is working, i can save all to SQL database and display it, i just down know how to multiple fields...

Here is my controler POST Code:

    public ActionResult Create()
    {
        ViewBag.zapStDoumenta_tk = new SelectList(db.dokumentGlava, "zapStDokumenta", "zapStDokumenta");
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "zapStPostavke,artikel,kolicina,cena,znesek,davek,popustNaPostavko,zapStDoumenta_tk")] postavkaDokumenta postavkaDokumenta)
    {
        if (ModelState.IsValid)
        {
            db.postavkaDokumenta.Add(postavkaDokumenta);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.zapStDoumenta_tk = new SelectList(db.dokumentGlava, "zapStDokumenta", "krajIzdaje", postavkaDokumenta.zapStDoumenta_tk);
        return View(postavkaDokumenta);
    }
Answers to

Summing values of two textboxes in Asp.net MVC

nr: #1 dodano: 2017-01-04 17:01

Try this javascript code. This should work.

$(function(){

  $("#kolicina,#cena").keyup(function(e){

  var val1=$("#kolicina").val(),
      val2=$("#cena").val(),
      result="";

  if(val1.length > 0){
    result += val1;
  }

  if(val2.length > 0){
    result += val2;
  }

  $("#znesek").val(result);

  });

});

Source Show
◀ Wstecz