I am trying to calculate sales tax in a view page and cannot seem to get it to work.
The code below is working other than producing a Total with sales Tax. The Sales Tax is singular and is not on every record. I basically just need to apply Tax to the subtotal and show the Total.
What I have tried:
totalOrder += (item.Quantity * item.SellingPrice);
total = (totalOrder * 8%);
It does not like the 8%
Using a Variable
var tax = .08;
var tax = 8%
((totalOrder * tax) / 100);
All if the above gives errors - Cannot use * with decimal etc.
Below is the code for that section of the view. Thanks for your help
<tbody>
@foreach (var item in Model.InvoiceDetail)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.Quantity)</td>
<td>@Html.DisplayFor(modelItem => item.Material)</td>
<td>@Html.DisplayFor(modelItem => item.Description)</td>
<td>$@Html.DisplayFor(modelItem => item.SellingPrice)</td>
<td>@string.Format("{0:C2}", (item.Quantity * item.SellingPrice))</td>
</tr>
totalOrder += (item.Quantity * item.SellingPrice);
total = (totalOrder * 8%);
}
</tbody>
</table>
<h5 style="margin-left: 80%;"><b>Sub Total :</b> @string.Format("{0:C2}", totalOrder)</h5>
<h5 style="margin-left: 80%;"><b>Tax :</b> 8%</h5>
<h5 style="margin-left: 80%;"><b>Total Due :</b> @string.Format("{0:C2}", total)</h5>
I also am using this:
decimal totalOrder = 0;
decimal total = 0;
[EDIT] Current code
decimal totalOrder = 100.1m;
decimal tax = .08m;
decimal total = totalOrder * (1.0m + tax);
<tbody>
@foreach (var item in Model.InvoiceDetail)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.Quantity)</td>
<td>@Html.DisplayFor(modelItem => item.Material)</td>
<td>@Html.DisplayFor(modelItem => item.Description)</td>
<td>$@Html.DisplayFor(modelItem => item.SellingPrice)</td>
<td>@string.Format("{0:C2}", (item.Quantity * item.SellingPrice))</td>
</tr>
//totalOrder += (item.Quantity * item.SellingPrice);
}
</tbody>
</table>
@{totalOrder += (Model.TotalCount * Model.PricePer);}
<h5 style="margin-left: 80%;"><b>Sub Total :</b> @string.Format("{0:C2}", totalOrder)</h5>
<h5 style="margin-left: 80%;"><b>Tax :</b> 8%</h5>
<h5 style="margin-left: 80%;"><b>Total Due :</b> @string.Format("{0:C2}", total)</h5>
Looks like you have two issues(and maybe one future issue), if I am interpreting your variable names correctly.
total
variable is only getting set to the tax value, and not the totalOrder
and the tax var tax = 8%
is not valid c#, you need to use a decimal like var tax = .08
The code should be something like:
decimal totalOrder = 100.01m;
decimal tax = .08m;
decimal total = totalOrder * (1.0m + tax);
Thanks to Nathan Werry I managed to get this problem resolved. I will explain what I did after each snippet.
Here is the code that was created for the Sales Tax:
decimal totalOrder = 0m;
decimal tax = .08m;
@{decimal totalTax = totalOrder * (tax);
decimal total = totalOrder + totalTax;}
TotalOrder just reflects that the Quantity * SellingPrice is calculated below and there is no need to add anything to it.
Tax is tax at .08m
TotalTax is the TotalOrder * Tax
And finally you have to add the tax to the TotalOrder.
Below is the finished code for the foreach statement:
<tbody>
@foreach (var item in Model.InvoiceDetail)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.Quantity)</td>
<td>@Html.DisplayFor(modelItem => item.Material)</td>
<td>@Html.DisplayFor(modelItem => item.Description)</td>
<td>$@Html.DisplayFor(modelItem => item.SellingPrice)</td>
<td>@string.Format("{0:C2}", (item.Quantity * item.SellingPrice))</td>
</tr>
totalOrder += (item.Quantity * item.SellingPrice);
}
</tbody>
</table>
@{decimal totalTax = totalOrder * (tax);
decimal total = totalOrder + totalTax;}
<h5 style="margin-left: 80%;"><b>Sub Total :</b> @string.Format("{0:C2}", totalOrder)</h5>
<h5 style="margin-left: 80%;"><b>Tax :</b> 8%</h5>
<h5 style="margin-left: 80%;"><b>Total Due :</b> @string.Format("{0:C2}", total)</h5>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.