makaroN
makaroN

Reputation: 315

Summing values of two textboxes in Asp.net MVC

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);
    }

Upvotes: 0

Views: 1600

Answers (1)

Nowshad Rahaman
Nowshad Rahaman

Reputation: 81

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);

  });

});

Upvotes: 3

Related Questions