Cleaner Conditional HTML Attributes In Razor Web Pages

@{

}

<!DOCTYPE html>
<
html lang="en">
<
head>
<
meta charset="utf-8" />
<
title></title>

</
head>
<
body>


     //Example for unordered list

   <ul>
<
li><a href="~/Page1" class="@IsCurrentPage("Page1")">Page 1</a></li>
<
li><a href="~/Page2" class="@IsCurrentPage("Page2")">Page 2</a></li>
<
li><a href="~/Page3" class="@IsCurrentPage("Page3")">Page 3</a></li>
</
ul>
      //Example for select combo

     <select>

        @foreach(var item in ViewBag.ItemList){
<option value="@Item.Id" @IsSelected("ItemId", @item.Id)>@item.Name
           </option>
}
     </select>

   <input type="checkbox" @IsChecked("IsApartments") /> Apartments

</body> </html>

@functions {
    public static bool Selected(int a, int b){
        return a == b;
}
    public string Iselected(string column, String value)

    {
       //Assume, you have sent a DataTable variable from controller to ViewBag.
       // If value matched with a specific column value, return “selected”.
        return ViewBag.DataTable.Rows[0][column].ToString() == value ? “selected” : null;
    }

    public string IsChecked(string columnName)
    {
       //Same as above function
        return Convert.ToBoolean( ViewBag.Retailer.Rows[0][columnName]) == true ? “checked” : null;
    }

}


Source: http://www.mikesdotnetting.com/Article/201/Cleaner-Conditional-HTML-Attributes-In-Razor-Web-Pages
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s