Monday, September 20, 2010

How to use a hidden input in MVC

While working on an e-commerce site, I needed to update my order total and submit it with the other form variables. My order total displayed to the user was just text, which doesn't get passed to the controller when submitted, so I needed a hidden input that I could update.

My html looks like this:

<p class="display_currency"><span id="totalprice">$0.00</span></p><%= Html.HiddenFor(m=>m.TotalOrder) %>

and my javascript. (snippet is from an UpdateTotal() function)

var total = 500.25
$("#totalprice").html("$" + total.toFixed(2));
$("#TotalOrder").val(total.toFixed(2)); //hidden control for model

when submitting the form, the model contains the correct value for TotalOrder

No comments:

Post a Comment