Amy
Amy

Reputation: 71

ASP.NET MVC3 C# - CSS by condition

Same foreach loop I have been posting about for days :) I'm almost done :)

I now need to style the differing show and hide posts so that if they are 'hide' they need to be red. So I research and I can to that in CSS with classes. Can anybody advise how to sort out an if statement?

@foreach
(var post in Model.tb_SH_Forum_Posts.OrderBy(o => o.Post_Date))
{
using (Html.BeginForm("Hide", "Post", new { id = post.Post_ID }))
{   
   <input type="submit" name = "hidePosts" value="Hide" /> 
}

using (Html.BeginForm("Show", "Post", new { id = post.Post_ID }))
{   
   <input type="submit" name = "showPosts" value="Show" /> 
} 

PSEUDO

if private_id = 2

<div class ="HIDE">
<fieldset>
        <p class="post_details">At @post.Post_Date By @(post.Anon == true ? "Anonymous" : post.Username)          
        </p>
        @post.Post_Desc
</fieldset>
</div>

ELSE

<div class ="SHOW">
<fieldset>
        <p class="post_details">At @post.Post_Date By @(post.Anon == true ? "Anonymous"    : post.Username)          
        </p>
        @post.Post_Desc
</fieldset>
</div>
}

As always, thank you for your time/guidance

Upvotes: 1

Views: 396

Answers (3)

bobek
bobek

Reputation: 8022

Ideally, you would like to create an HTML helper to handle that, since no logic should be used in Views.

http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs

Here is a Microsoft's article about custom helpers. Basically it's a class, where you pass something from the view, and then you include all the logic in there, and return something back to your view.

Upvotes: 0

Kevin Cloet
Kevin Cloet

Reputation: 2976

    @if (private_id == 2)
    {
        <div class ="HIDE">
    }else
    {
        <div class ="SHOW">
    }

Upvotes: 1

Andreas Eriksson
Andreas Eriksson

Reputation: 9027

Try this:

<div <%: private_id == 2 ? "class=HIDE" : "class=SHOW" %> >

Upvotes: 1

Related Questions