我想知道我想做什么在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))); }
}
我经过了很多变量的查询字符串,以减少看法表单字段的数量。 我设法做了这种编码,但它跳过了前两个值并只保存了数据库中最后一个正确的条目。我需要专家的帮助
发布您的控制器和型号代码 – Nighil
@Nighil我已经发布控制器和型号,请您 – Diin