-1
\$\begingroup\$

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">&nbsp;@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">&nbsp;@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>
\$\endgroup\$
3
  • \$\begingroup\$ Fixed question with a concrete example. \$\endgroup\$ Commented Oct 14, 2016 at 2:17
  • 2
    \$\begingroup\$ As we all want to make our code more efficient or improve it in one way or another, try to write a title that summarizes what your code does, not what you want to get out of a review. \$\endgroup\$ Commented Oct 14, 2016 at 5:36
  • \$\begingroup\$ This question sounds rather opinion-based and, as you have currently worded it, hypothetical. \$\endgroup\$ Commented Oct 14, 2016 at 12:51

1 Answer 1

1
\$\begingroup\$

I'd say that if you feel the need to do this, you may have need of a partial view. A modular UI is key to maintainability. For example, in your code, create a new partial:

ReceiptDetails.csthml

@model YourReceiptClass
<div class="col-xs-9">
    <label>@Translate.Text("receipt-number")</label> @Model.ID.Value.ToString()<br/>
    <label>@Translate.Text("club-number-colon")</label> @Model.BillToCustomer.Value<br/>
    <label>@Translate.Text("club-name-colon")</label> @Model.BillToName<br/>
    <label>@Translate.Text("date-with-colon")</label> @Model.PaymentDate.ToShortDateString()<br/>
    <label>@Translate.Text("payment-type-colon")</label> @Model.PaymentMethod<br/>
    <label>@Translate.Text("credit-card-number-colon")</label> @Model.PaymentReferenceNumber<br/>
    <label>@Translate.Text("name-on-card")</label> @Model.PayerName<br/>
</div>

Then replace it in your other view with:

 @{ Html.RenderPartial("~/path/ReceiptDetails.cshtml", Model.Receipt); }

Then you've got a reusable chunk of HTML and you now don't need to worry about your original concern.

As a side note, there may be a more semantically correct choice than using <label> here. For example, a definition list could be appropriate.

Also, don't use <br/>, style the elements with CSS to be block.

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.