繁体   English   中英

C# ASP.NET MVC:提交按钮后不会存储数据

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

我正在学习 C# 和 ASP.NET MVC,我需要这方面的帮助。

单击@html.EditorFor提交按钮“Sacuvaj”文本不会更改后,我需要将“plata”中的数据放入该文本框中, @ViewBag.Message工作正常,单击“Sacuvaj”按钮后收到消息具有良好的数据,但在编辑器中不会输入,我需要“fondCasova”中的数字与“183”成为 * 并将结果发布到“brutoPlata”中,谢谢。

https://prnt.sc/1wk0frz

这是视图:

@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>
}  

这是控制器代码:

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

这是模型类:

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

你所有的return View()语句都是空的; 您没有将模型(obm)发送回视图。 Young Shung 有说过你应该像这样通过模型吗?

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

...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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