Avoiding redundant code in MVC view page 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")
    {
        &lt;div class="summaryTab"&gt;
    &lt;ul class="nav nav-tabs" data-tabs="tabs"&gt;
        &lt;li&gt;&lt;a data-toggle="tab" href="#heat"&gt;Heat Map&lt;/a&gt;&lt;/li&gt;
        &lt;li class="active"&gt;&lt;a data-toggle="tab" href="#table"&gt;Table&lt;/a&gt;&lt;/li&gt;

    &lt;/ul&gt;
&lt;/div&gt;
&lt;div class="tab-content"&gt;
    &lt;div class="tab-pane active" id="table1"&gt;
        @Html.Partial("~/Views/Shared.MultiQuery/_resultset.cshtml", Model)
    &lt;/div&gt;
    &lt;div class="tab-pane" id="heat1"&gt;
    &lt;/div&gt;

&lt;/div&gt;
    }
    else{
        &lt;div class="summaryTab"&gt;
    &lt;ul class="nav nav-tabs" data-tabs="tabs"&gt;
        &lt;li class="active"&gt;&lt;a data-toggle="tab" href="#heat"&gt;Heat Map&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a data-toggle="tab" href="#table"&gt;Table&lt;/a&gt;&lt;/li&gt;

    &lt;/ul&gt;
&lt;/div&gt;
&lt;div class="tab-content"&gt;
    &lt;div class="tab-pane" id="table2"&gt;        
    &lt;/div&gt;
    &lt;div class="tab-pane active" id="heat2"&gt;
        @Html.Partial("~/Views/Shared.MultiQuery/_resultset.cshtml", Model)
    &lt;/div&gt;

&lt;/div&gt;
    }
}
else
{
&lt;div class="summaryTab"&gt;
    &lt;ul class="nav nav-tabs" data-tabs="tabs"&gt;
        &lt;li&gt;&lt;a data-toggle="tab" href="#heat"&gt;Heat Map&lt;/a&gt;&lt;/li&gt;
        &lt;li class="active"&gt;&lt;a data-toggle="tab" href="#table"&gt;Table&lt;/a&gt;&lt;/li&gt;

    &lt;/ul&gt;
&lt;/div&gt;
&lt;div class="tab-content"&gt;
    &lt;div class="tab-pane active" id="table"&gt;
        @Html.Partial("~/Views/Shared.MultiQuery/_resultset.cshtml", Model)
    &lt;/div&gt;
    &lt;div class="tab-pane" id="heat"&gt;
    &lt;/div&gt;

&lt;/div&gt;
}
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 &amp;&amp; activeTab.ToLower() != "index") {
        heatClass = "active";
        heatTabContent = tabContent;
        tableId += "2";
        heatId += "2";
    } else {
        tableClass = "active";
        tableTabContent = tabContent;
        if (savingHtml) {
            tableId += "1";
            heatId += "1";
        }
    }
}

&lt;div class="summaryTab"&gt;
  &lt;ul class="nav nav-tabs" data-tabs="tabs"&gt;
    &lt;li class="@heatClass"&gt;&lt;a data-toggle="tab" href="#heat"&gt;Heat Map&lt;/a&gt;&lt;/li&gt;
    &lt;li class="@tableClass"&gt;&lt;a data-toggle="tab" href="#table"&gt;Table&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;div class="tab-content"&gt;
    &lt;div class="tab-pane active" id="@tableId"&gt;
        @tableTabContent
    &lt;/div&gt;
    &lt;div class="tab-pane" id="@heatId"&gt;
        @heatTabContent
    &lt;/div&gt;

&lt;/div&gt;



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...