3
\$\begingroup\$

Based on the true condition, I am making some operations. But I want to simplify the below HTML code in a better way.

@{
 bool savingHtml = (Request.QueryString["savehtml"] == "1");
 string activeTab = Request.QueryString["from"];
}
@if (savingHtml)
{
 if (activeTab.ToLower() == "index")
 {
 <div class="summaryTab">
 <ul class="nav nav-tabs" data-tabs="tabs">
 <li><a data-toggle="tab" href="#heat">Heat Map</a></li>
 <li class="active"><a data-toggle="tab" href="#table">Table</a></li>
 </ul>
</div>
<div class="tab-content">
 <div class="tab-pane active" id="table1">
 @Html.Partial("~/Views/Shared.MultiQuery/_resultset.cshtml", Model)
 </div>
 <div class="tab-pane" id="heat1">
 </div>
</div>
 }
 else{
 <div class="summaryTab">
 <ul class="nav nav-tabs" data-tabs="tabs">
 <li class="active"><a data-toggle="tab" href="#heat">Heat Map</a></li>
 <li><a data-toggle="tab" href="#table">Table</a></li>
 </ul>
</div>
<div class="tab-content">
 <div class="tab-pane" id="table2"> 
 </div>
 <div class="tab-pane active" id="heat2">
 @Html.Partial("~/Views/Shared.MultiQuery/_resultset.cshtml", Model)
 </div>
</div>
 }
}
else
{
<div class="summaryTab">
 <ul class="nav nav-tabs" data-tabs="tabs">
 <li><a data-toggle="tab" href="#heat">Heat Map</a></li>
 <li class="active"><a data-toggle="tab" href="#table">Table</a></li>
 </ul>
</div>
<div class="tab-content">
 <div class="tab-pane active" id="table">
 @Html.Partial("~/Views/Shared.MultiQuery/_resultset.cshtml", Model)
 </div>
 <div class="tab-pane" id="heat">
 </div>
</div>
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Mar 5, 2014 at 18:07
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Most of your code is duplicated, what I did is extract the logic to the first code block, and left only the placeholders in the HTML.

@{
 bool savingHtml = (Request.QueryString["savehtml"] == "1");
 string activeTab = Request.QueryString["from"];
 string tabContent = Html.Partial("~/Views/Shared.MultiQuery/_resultset.cshtml", Model);
 string heatClass, tableClass;
 string tableId = "table";
 string heatId = "heat";
 string heatTabContent, tableTabConten;
 if (savingHtml && activeTab.ToLower() != "index") {
 heatClass = "active";
 heatTabContent = tabContent;
 tableId += "2";
 heatId += "2";
 } else {
 tableClass = "active";
 tableTabContent = tabContent;
 if (savingHtml) {
 tableId += "1";
 heatId += "1";
 }
 }
}
<div class="summaryTab">
 <ul class="nav nav-tabs" data-tabs="tabs">
 <li class="@heatClass"><a data-toggle="tab" href="#heat">Heat Map</a></li>
 <li class="@tableClass"><a data-toggle="tab" href="#table">Table</a></li>
 </ul>
</div>
<div class="tab-content">
 <div class="tab-pane active" id="@tableId">
 @tableTabContent
 </div>
 <div class="tab-pane" id="@heatId">
 @heatTabContent
 </div>
</div>
  • Note: I have no experience with ASP.Net, so I apologize if the code is not as clean as possible, or has any syntax errors... The gist of the matter is still that if you extract the logic, you get shorter and cleaner code, with much less copy+paste...
answered Mar 5, 2014 at 19:44
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.