繁体   English   中英

从mvc3控制器提交计算值到数据库

[英]committing calculated values to database from mvc3 controller

我想知道我想做的事在MVC3 Controller中是否可行。 我有3张桌子; 会员,付款方式和薪资类别。 我在类别表中有3种不同的付款类别(CatA,CatB和CatC)。 付款视图中的表格用于接受会员ID和付款值(十进制)。 我希望做的是将一个值通过表单传递给控制器​​时,该值按比例分成3个,对于每个类别,计算得出的值,并将相同的成员ID提交给数据库。 例如,如果CatA为30%,CatB为50%,CatC为20%,成员ID为1010,并且100.00传递给控制器​​,则应将以下内容提交给数据库

pId | mId | catid |

1 | 1010 | 1 | 30.00

2 | 1010 | 2 | 50.00

3 | 1010 | 3 | 20.00

任何线索或任何形式的帮助将不胜感激。 控制者

    // POST: /AutoPay/Create
    [HttpPost]
    public ActionResult Create(AutoPay autopay, int tcd, int id, int tzid, FormCollection formCollection)
    {
        if (ModelState.IsValid)
        {

         string[] rawpaysplit = formCollection["PayCatSplit"].Split(' ');
          foreach (var x in rawpaysplit){             

          if (x.Equals (rawpaysplit[0])){
          autopay.PId = 3;}
          if (x.Equals (rawpaysplit[1])){
          autopay.PId = tzid;}   
          if (x.Equals (rawpaysplit[2])){
          autopay.PId = 4;}
          autopay.Pay= Convert.ToDecimal(x);
          autopay.UId = id;
          autopay.Tcd = tcd;
          autopay.PDate = DateTime.Now;
          autopay.LastUpdate = DateTime.Now;
            autopay.PEntryId = Membership.GetUser().UserName;
            db.AutoPays.Add(autopay);

            db.SaveChanges();
 }
            return RedirectToAction("Index");
        }

模型

 public class AutoPay
{
    [Key]
    public int Id { get; set; }

    public int? Tcd { get; set; }
    public int UId { get; set; }
    public virtual Member Member { get; set; }
    public int PId { get; set; }
    public virtual PayCat PayCat { get; set; }
    public int YId { get; set; }
    public virtual DateYear DateYear { get; set; }
    public int MId { get; set; }
    public virtual DateMonth DateMonth { get; set; }
    [DisplayFormat(DataFormatString = "{0:F2}", ApplyFormatInEditMode = true)]
    [DisplayName("Amount")]
    public decimal Pay { get; set; }
    [ScaffoldColumn(false)]
    public DateTime PDate { get; set; }
    [DisplayName("Entry By")]
    public string PEntryId { get; set; }
    [DisplayName("last Upadated By")]
    public string PUpdatedBy { get; set; }
    [DisplayName("Receipt No")]
    public string RecNo { get; set; }
    [DisplayName("Book No")]
    public int BId { get; set; }
    public virtual ReceiptBook ReceiptBook { get; set; }
    public bool POK { get; set; }
    [ScaffoldColumn(false)]
    public DateTime LastUpdate { get; set; }
    public string PayCatSplit
    {
        get { return string.Format("{0} {1} {2}", Math.Round((this.Pay * 0.773211m), 2), Math.Round((this.Pay * 0.123703m),2), Math.Round((this.Pay * 0.103086m))); }
    }

我在querystring中传递了很多变量,以减少视图表单中的字段数。 我设法进行了编码,但是它跳过了前两个值,仅将最后一个正确的条目保存在数据库中。 我需要专家的帮助

当然可以。 在MVC上,您可以执行通常在其他框架上可以执行的任何编程。

但是,MVC模式的范式要求您以不同的方式思考通常如何在更直接的框架上完成。

在这里,我将向您提示一些信息。 不要使用控制器进行计算或数据操作。 只需使用模型类来做到这一点,并让您的控制器调用该类并使用它,然后尝试将您的控制器维护为专用于在视图和模型类之间分配程序执行和数据的方法,就可以使这些方法持久地工作。

因此,当然,您可以将视图中的任何数据传递给控制器​​,使用控制器将该值传递给模型类,在模型类中您需要对数据进行任何计算或操作并将其保存到数据库。

暂无
暂无

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

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