さて、私はこのような複雑なフォームビューモデルを持っています:
public class TransactionFormViewModel
{
public Session SessionRecord { get; private set; }
public IEnumerable<Resource> ResourcePerSessionRecord { get; private set; }
public Person PersonRecord { get; private set; }
public decimal SubTotal { get; private set; }
public TransactionFormViewModel(Person patient, Session session, IEnumerable<Resource> resourcePerSession)
{
this.PersonRecord = person;
this.SessionRecord = session;
this.ResourcePerSession = resourcePerSession
this.SubTotal = CalculateSubTotal();
}
private decimal CalculateSubTotal()
{
return ResourcePerSession.Sum(x => x.Cost);
}
}
これは私のモデルで、ビューで使用します。ビューは次のようになります。
<A table that shows Person data></table>
<A table that shows a review of the Session> </table>
<!-- the submit button i need to complete the transaction -->
<% using (Html.BeginForm("PayNow", "Session"))
{ %>
<div id="trans-ses-footer">
<%:Html.HiddenFor(x => x.SessionRecord.SessionID) %>
<%:Html.HiddenFor(x => x.SessionRecord.PersonID) %>
<%:Html.HiddenFor(x => x.SubTotal) %>
<input type="submit" value="Pay" />
</div>
<% } %>
</div>
コントローラーは次のようになります。 [HttpPost] public ActionResult PayNow() { //トランザクション モデル Transaction transaction = new Transaction();
transaction.SessionID = int.Parse(Request.Form["SessionRecord.SessionID"]);
transaction.PersonID = int.Parse(Request.Form["SessionRecord.PersonID"]);
transaction.TotalCost = decimal.Parse(Request.Form["SubTotal"]);
transaction.Paid = true;
_sessionRepository.SaveTransaction(transaction);
TempData["TransactionMessage"] = "The transaction was saved successfully.";
return View();
}
トランザクションに必要な値を取得するために Request.Form を使用しています。私はこのようにしたい場合:
Public ActionResult(Transaction transaction)
{
if(ModelState.IsValid)
_transactionRepository.SaveTransaction(transaction)
etc...
}
カスタム モデル ビッダーを作成する必要があると思います。苦労する価値はありますか?パフォーマンスやその他の面で何か得られることはありますか? または、この種のことを行うことができる他の方法を知っていますか? このシナリオを正しく表現する方法がわからないため、関連するものが見つかりませんでした...よろしくお願いします。