我想知道我想做什麼在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