0

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);
    }
4
  • 3
    Show your js code.. Commented Jan 4, 2017 at 16:14
  • Have you got form tags? (ie @Html.BeginForm). Show the complete View code, and the controller method that you're POSTing to. Commented Jan 4, 2017 at 16:17
  • I edited my original post. Commented Jan 4, 2017 at 16:27
  • If you want to keep that dynamically updated, then you're going to have to use JavaScript, and .NET MVC won't help you at all. The only thing C# will help with is sending an initial value inside the textbox from the server. But again, JavaScript would be a better idea. Commented Jan 4, 2017 at 16:37

1 Answer 1

3

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

  });

});

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.