在Asp.net MVC中汇总两个文本框的值

时间:2017-01-04 16:11:32

标签: c# asp.net asp.net-mvc razor

我是ASP .NET MVC Web Aplications的新手,我需要一些帮助。我需要总结两个值(" Cena"和" Kolicina")并保存到文本框" Znesek"。我该怎么办?我试过这个(How to multiply values of two textboxes in Asp.net MVC),但它不适合我:/

这是我的.cshtml文件:

    <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

请帮助我,我需要改变什么或需要采取哪些措施?

编辑: 当然,我有@ Html.BeginForm以及View代码所需的一切,这只是部分代码。一切正常,我可以保存所有SQL数据库并显示它,我只知道如何多个字段......

这是我的控制器POST代码:

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

1 个答案:

答案 0 :(得分:3)

试试这个javascript代码。这应该有用。

&#13;
&#13;
$(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);

  });

});
&#13;
&#13;
&#13;