私がやりたいことがMVC3コントローラーで実現可能かどうか疑問に思っています。3 つのテーブルがあります。メンバー、支払い、および支払いカテゴリ。カテゴリ テーブルに 3 つの異なる支払いカテゴリ (CatA、CatB、および CatC) があります。支払いビューには、メンバー ID と支払い値 (10 進数) を受け入れるためのフォームがあります。私がやりたいことは、値がフォームを介してコントローラに渡され、その値が比率で3つに分割され、カテゴリごとに計算された値が返され、同じメンバーIDがデータベースにコミットされることです。たとえば、CatA が 30%、CatB が 50%、CatC が 20% で、メンバー ID が 1010 で、100.00 がコントローラーに渡される場合、以下をデータベースにコミットする必要があります。
pId | ミッド ID | ネコ科 | 価値
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))); }
}
ビュー フォームのフィールド数を減らすために、クエリ文字列に多くの変数を渡しました。私はこのコーディングを行うことができましたが、最初の 2 つの値をスキップし、最後の正しいエントリのみをデータベースに保存します。専門家の助けが必要です