Jordan Axe
Jordan Axe

Reputation: 3923

How would I do this the best way?

I got a viewmodel with the following attributes:

int Balance
int WithdrawAmount

A view of mine in my ASP.NET MVC4 application lets the user withdraw money from their balance. Now when the page is accessed through a GET request I get the users balance and pass it to the view through a WithdrawViewModel thus displaying it to the user - in the model the withdrawAmount is since the user haven't specified the amount to withdraw yet.

The user enters the desired amount to withdraw and submits a POST request sending the withdraw amount.

Now I bind the withdrawAmount to a WithdrawViewModel again in order to automatically check if the modelstate is valid (is the amount above 0).

Finally I withdraw the amount etc.

The thing I wanna know is if there's a better way to accomplish this? To me it seems foolish to use the same viewmodel when the values clearly differ (either the balance is in the model or the withdrawAmount is in the model) - virtually making them 2 different models.

Can I do it in a better way?

Upvotes: 0

Views: 59

Answers (1)

Rob
Rob

Reputation: 5588

If you only need the WithdrawAmount, just have the Controller Action method take an int parameter.

[HttpPost]
[Authorize]
public ViewResult MyMethod(int withdrawAmount){ ... }

Upvotes: 1

Related Questions