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

Posted

技术标签:

【中文标题】C# ASP.NET MVC:提交按钮后不会存储数据【英文标题】:C# ASP.NET MVC : won't store data after submit button 【发布时间】:2021-12-05 03:22:00 【问题描述】:

我正在学习 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; 
    

【问题讨论】:

对于您的 POST 方法,传递 obm 以使用 return View(obm); 查看。 很抱歉,我找不到提交按钮“Sacuvaj” @YongShun 谢谢你的意见,没有解决,还是不行 嗨@Serge,我把它放在“视图”表单的末尾,向下滚动你会看到 【参考方案1】:

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

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

...

【讨论】:

感谢您的评论,仍然无法正常工作,仅在 viewbag.message 框中返回我的更改【参考方案2】:

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

【讨论】:

以上是关于C# ASP.NET MVC:提交按钮后不会存储数据的主要内容,如果未能解决你的问题,请参考以下文章

提交按钮未在 ASP.NET MVC Web 应用程序中触发

在 ASP.NET MVC Core(C#) 中执行 Oracle 存储过程 (PL/SQL)

如何在 ASP.NET MVC 中 RedirectToAction 而不会丢失请求数据

ASP.NET MVC Core/6:多个提交按钮

ASP.NET MVC Core/6:多个提交按钮

带有 Jquery 模态形式的 asp.net MVC