2012-10-04 26 views
0

我想知道我想做什么在MVC3控制器中是可行的。我有3张桌子;会员,付款和支付类别。我在分类表(CatA,CatB和CatC)中有3种不同的付款类别。付款视图是接受会员ID和付款金额(十进制)的表单。我想要做的是,当一个值通过表单传递给控制器​​时,值按比例拆分为3,并且每个类别都是计算值,并且同一个成员标识被提交给数据库。例如,如果CatA是30%,CatB是50%并且CatC是20%并且成员Id是1010并且100.00被传递到控制器,则以下应该被提交到数据库从mvc3控制器提交计算值到数据库

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

我经过了很多变量的查询字符串,以减少看法表单字段的数量。 我设法做了这种编码,但它跳过了前两个值并只保存了数据库中最后一个正确的条目。我需要专家的帮助

+0

发布您的控制器和型号代码 – Nighil

+0

@Nighil我已经发布控制器和型号,请您 – Diin

回答

0

当然,你可以做到这一点。在MVC上,你可以做任何你通常可以在其他框架上做的编程。

然而,MVC模式的范式需要你想怎么它通常是更直接的框架做了不同的方式。

在这里,我要提醒你的东西。请勿使用您的控制器进行计算或数据操作。只需使用一个模型类来做到这一点,并让你的控制器调用该类并使用它,尝试将控制器维护为专用于在视图和模型类之间分发程序执行和数据的方法,让这些持续艰苦的工作。

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

+0

谢谢@Bardo我正在做更多的研究。目前我能够使用传统的mvc4 scafolding和模板来实现目标,但我必须一次插入一条记录。如果我能够实现我想要的功能,那么将会大大节省用户时间。 – Diin

+0

尝试将更改保存在循环中而不是外部循环中。移动db.SaveChanges();到你的里面。 – Bardo

+0

谢谢修复它 – Diin

相关问题