views:

211

answers:

1

Well i have a complex form view model like this :

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

This is my Model, which i use in the view, which (view) looks like this:

        <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>

The controller looks like this: [HttpPost] public ActionResult PayNow() { //the transaction model 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();
    }

I'm using Request.Form in order to get the values i need for the transaction. If i want to do it like this :

Public ActionResult(Transaction transaction)
{
  if(ModelState.IsValid) 
       _transactionRepository.SaveTransaction(transaction) 
  etc...
}

i need to create a custom model bidder i guess. Is it worth the trouble? Will i gain anything in performance or in any other aspect? Or do you know any other method that i can do this kind of thing? I dont know how to express it correctly this scenario, so i couldn't find anything relevant... Thank you in advance.

+2  A: 

In my opinion it's always worth creating a custom model binder when the default binder can't handle your case. It will do exactly what you are doing in your action anyway, so there will be no extra code (just a extra class). And you will get rid of the dependency on the httpcontext and have a clearer action method.

Mattias Jakobsson
Clearer actions is what i'm after. I tried the approach to use a custom ViewModel for postback, which made the code a bit clearer (public ActionResult Payment(TransactionPostViewModel viewModel)) without the httpcontext calls, but i had to populate a Transaction entity again "by hand". I guess custom binding is one way road for this kind of thing...Thank you!
goldenelf2