简体   繁体   中英

C# ASP.NET MVC : won't store data after submit button

I am learning C# and ASP.NET MVC, I need a help with this.

After clicking submit button "Sacuvaj" text in @html.EditorFor won't change, I need to put data from "plata" in that text box, @ViewBag.Message work fine, and I get the message after clicking button "Sacuvaj" with good data, but in editor won't enter, I need the number from "fondCasova" to be * with "183" and to post that result in "brutoPlata" thanks.

https://prnt.sc/1wk0frz

Here is the view:

@model ObracunZarade.Models.obracunModel

@using (Html.BeginForm("DodajObracun", "obracun",FormMethod.Post))
{
<div class="form-horizontal">
    <h4>Add Employee</h4>
    <hr />

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

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

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

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

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

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

        </div>
    </div>

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

        </div>
    </div>

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

        </div>
    </div>

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

        </div>
    </div>

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

        </div>
    </div>

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

    <!--  <div class="form-group">
        @Html.LabelFor(model => model.BrutoPlata, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
                @Html.EditorFor(model => model.BrutoPlata, new { htmlAttributes = new { @class = "form-control" } })
</div>
    </div>
    -->
    @Html.EditorFor(model => model.BrutoPlata)
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Sacuvaj" class="btn btn-default" />a
        </div>
    </div>
    <!--<div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="button" name="izracunaj" value="Izracunaj" class="btn btn-default" />
        </div>
    </div>-->
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10" style="color:green">
            @ViewBag.Message

        </div>
    </div>
</div>
<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="button" name="izracunaj" value="Izracunaj" class="btn btn-default" />
    </div>
</div>
}  

Here is the controller code:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using ObracunZarade.Models;
using System.Linq;
using System.Web.Mvc;

namespace ObracunZarade.Controllers
{
    public class obracunController : Controller
    {
        private SqlConnection conn;

        private void connection()
        {
            string constr = "data source=desktop-kmn8om7\\mssqlserver98;initial catalog=obracunBaza;integrated security=True;";
            conn = new SqlConnection(constr);
        }

        public ActionResult dodajObracun()
        {
            return View();
        }
        
        [HttpPost]
        public ActionResult dodajObracun(obracunModel obm, string btn)
        {
            if(btn == "Izracunaj")
            {
                ViewBag.Message = "KLIKNUO SI IZRACUNAJ";
                return View();
            }

            try
            {
                if(ModelState.IsValid)
                {
                    /*if(dodajObracunBool(obm))
                    {
                        ViewBag.Message = "Obracun je upisan u bazu";
                    }*/
                }
 
                decimal plata = obm.fondCasova * 183;
                obm.osnNetoZarada = plata;
                
                obm.netoOsnovica = 100;
                ViewBag.Message = plata;
                obm.BrutoPlata = plata;
 
               return View();
            }
            catch
            {
                ViewBag.Message = "Obracun nije upisan u bazu";
                return View();
            }
        }

        [HttpPut]
        public bool dodajObracunBool(obracunModel obm)
        {
            //if () 
            connection();

            SqlCommand c = new SqlCommand("SQLQuery6", conn);
            c.CommandType = CommandType.StoredProcedure;

            c.Parameters.AddWithValue("@Ime", obm.Ime);
            c.Parameters.AddWithValue("@Prezime", obm.Prezime);
            c.Parameters.AddWithValue("@Adresa", obm.Adresa);
            c.Parameters.AddWithValue("@fondCasova", obm.fondCasova);
            c.Parameters.AddWithValue("@osnNetoZarada", obm.osnNetoZarada);
            c.Parameters.AddWithValue("@iznPoreza", obm.iznPoreza);
            c.Parameters.AddWithValue("@iznDoprinosaPIO", obm.iznDoprinosaPIO);
            c.Parameters.AddWithValue("@iznDoprinosaZaZdrav", obm.iznDoprinosaZaZdrav);
            c.Parameters.AddWithValue("@iznDoprinosaZaNezap", obm.iznDoprinosaZaNezap);
            c.Parameters.AddWithValue("@netoOsnovica", obm.netoOsnovica);
           // c.Parameters.AddWithValue("@BrutoPlata", obm.BrutoPlata);

            conn.Open();
            int i = c.ExecuteNonQuery();
            conn.Close();

            if (i >= 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

And here is the model class:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ObracunZarade.Models
{
    public class obracunModel
    {
        public int obracunID { get; set; }
        
        public string Ime { get; set; }
        public string Prezime { get; set; }
        public string Adresa { get; set; }

        public int fondCasova { get; set; }

        public decimal osnNetoZarada { get; set; }
        public decimal iznPoreza { get; set; }
        public decimal iznDoprinosaPIO { get; set; }
        public decimal iznDoprinosaZaZdrav { get; set; }
        public decimal iznDoprinosaZaNezap { get; set; }
        public decimal netoOsnovica { get; set; }
        public decimal BrutoPlata { get; set; }
    }
}

All your return View() statements are empty; you are not sending the model (obm) back to the view. Has Young Shung said you should pass the model like this?

ViewBag.Message = plata;              
obm.BrutoPlata = plata;                      
return View(obm);                     
enter code here

...

我通过从 @html.EditorFor 更改为 @html.DisplayFor 解决了这个问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM