Let's say the model for a razor view has the following name:
Model.Receipt
and there are a couple of places where you do
@Model.Receipt.ID
@Model.Receipt.BillToCustomer
etc.
but you decide to shorten it by declaring the following code block:
@{ var receipt = Model.Receipt;} and now you use this receipt variable instead.
Is there any advantage to this? Any disadvantages?
Here is the concrete example below:
@using System.Web.Mvc.Html
@using Sitecore.Globalization
@using Sitecore.Mvc
@using TI.Web.Models
@using TI.Web.Models.ClubCentral.ClubFinancials
@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<ViewReceiptModel>
@{
var receipt = @Model.Receipt;
}
<div class="container">
@Html.Partial("~/Views/Partials/PageHeader.cshtml", new PageHeader(Html.Sitecore().Field("Title").ToString()))
<div class="row">
<div class="col-xs-9">
<label>@Translate.Text("receipt-number")</label> @receipt.ID.Value.ToString()<br/>
<label>@Translate.Text("club-number-colon")</label> @receipt.BillToCustomer.Value<br/>
<label>@Translate.Text("club-name-colon")</label> @receipt.BillToName<br/>
<label>@Translate.Text("date-with-colon")</label> @receipt.PaymentDate.ToShortDateString()<br/>
<label>@Translate.Text("payment-type-colon")</label> @receipt.PaymentMethod<br/>
<label>@Translate.Text("credit-card-number-colon")</label> @receipt.PaymentReferenceNumber<br/>
<label>@Translate.Text("name-on-card")</label> @receipt.PayerName<br/>
</div>
<div class="col-xs-3">
<a class="btn-financials" href="@Model.ExportURL" target="_blank">
<span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span>
<span aria-label="Right Align"> @Translate.Text("financials-export-button-text")</span>
</a>
<a class="btn-financials" href="@Model.PrintURL" target="_blank">
<span class="glyphicon glyphicon-print" aria-hidden="true"></span>
<span aria-label="Right Align"> @Translate.Text("Print")</span>
</a>
</div>
</div>
<br/>
<div class="row">
<div class="col-xs-12">
<div class="table-responsive receipt-headers">
<table class="table">
<thead>
<tr>
<th>@Translate.Text("order-pound-sign")</th>
<th>@Translate.Text("name")</th>
<th>@Translate.Text("order-description")</th>
<th>@Translate.Text("amount")</th>
</tr>
</thead>
<tbody>
@foreach (var associatedOrder in receipt.AssociatedOrders)
{
var orders = receipt.Lines.Where(x => Equals(x.OrderID, associatedOrder.ID));
foreach (var order in orders)
{
<tr>
<td>@order.OrderID.Value.ToString()</td>
<td>@associatedOrder.ShipToName</td>
<td> @order.Description</td>
<td>@order.Amount.ToString("C")</td>
</tr>
}
}
</tbody>
<tfoot>
<tr>
<td colspan="3" style="text-align: right;">
@Translate.Text("receipt-total")
</td>
<td>
<label>
@receipt.Lines.Sum(x => x.Amount).ToString("C")
</label>
</td>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
@Html.Raw(@Translate.Text("receipt-footer-message"))
</div>
</div>
@Html.Partial("~/Views/Partials/FooterClearFix.cshtml")
</div>