我正在學習 C# 和 ASP.NET MVC,我需要這方面的幫助。
單擊提交按鈕“Sacuvaj”中的文本@html.EditorFor不會改變后,我需要將“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; }
}
}
uj5u.com熱心網友回復:
你所有的return View()陳述都是空的;您沒有將模型(obm)發送回視圖。Young Shung 有說過你應該像這樣通過模型嗎?
ViewBag.Message = plata;
obm.BrutoPlata = plata;
return View(obm);
enter code here
...
uj5u.com熱心網友回復:
我通過從 @html.EditorFor 更改為 @html.DisplayFor 解決了這個問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/327292.html
標籤:C# html 。网 asp.net-mvc
上一篇:并行分發請求
