2

Ultimately I would like to have calculated fields like Weight * Price.

Suppose I have in my MVC3 view:

    @Html.TextBoxFor(modelItem => item.Package.USDKg)
    @Html.HiddenFor(modelItem => item.Pricing.VolumePrice)

Am I able to calculate these directly? Or resort to JQuery? I would really like to keep pricing "hidden" even in source view if possible.

Basically, when a user enters their package weight, a price would be displayed (calculated off of hidden price point values).

Thank you for your guidance.

Par6
  • 389
  • 1
  • 10
  • 20

2 Answers2

2

Having a razor, you could do a logic just inside the mark up!

@{
   var value = model.Item.Value;
   var price = model.Item.Proce;
   var calculated = value * price;
}

<div class="price>Your price: @calculated</div>

More info is here:

http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx

Alexander Beletsky
  • 19,453
  • 9
  • 63
  • 86
  • Thank you! That would be super but I am now facing "Cannot assign lambda expression to an implicitly-typed local variable". There's an essay here: http://www.interact-sw.co.uk/iangblog/2008/03/17/lambda-inference but wondering if there had been any changes since then to make life easier. – Par6 Jul 10 '11 at 07:41
  • @Par6 please carefully read the error message and check the place where it is pointing.. if think you would be able to fix that :) if no, please create new question.. if my answer helped you, please mark it as "correct" – Alexander Beletsky Jul 10 '11 at 10:05
1

Yes, you could do it like this:

function calculate(){
   var weight = parseFloat($("#Package_USDKg").val());
   var price = parseFloat($("#Pricing_VolumePrice").val());
   var calculus = weight * price;
   $("#calculus").text(calculus); //This shows the calculated field
}

$(document).ready(function(){
  calculate(); //Calculate on page load
  $("#Package_USDKg").change(calculate);  //Calculate every time weight changes
});

I'm assuming you have a div with id="calculus" to show the calculated field.

Hope this helps. Cheers

Edgar Villegas Alvarado
  • 18,204
  • 2
  • 42
  • 61