1

I have the following code in my view:

<%= Html.DropDownListFor(r=>r.Id, Model.MyList, new { @class = "Dropdown hidden" })%>

but now I want to only assign the class "hidden" in certain cases so I want to pass that in from my ViewModel to either add the class or to leave it out.

To solve this, I created a property in my view model called HiddenConditional

  public string HiddenConditional
  {
      get
      {
          return IfCondition() ? "hidden" : string.empty;
      }
  }

but i can't figure out the right syntax to put that into the class attribute. I need something like:

<%= Html.DropDownListFor(r=>r.Id, Model.MyList, new { @class = "Dropdown <%=Model.HiddenConditional%>" })%>
1
  • Maybe I'm thinking too simply, but couldn't you set a property in the ViewModel to true or false, then in the DropDownListFor, do this: <%= Html.DropDownListFor(r=>r.Id, Model.MyList, new { @class = "Dropdown " + (Model.MyListIsHidden ? " hidden" : "") })%> Commented Mar 7, 2015 at 17:27

2 Answers 2

1

You need to do this:

<%= Html.DropDownListFor(r=>r.Id, Model.MyList, 
               new { @class = "Dropdown " + Model.HiddenConditional })%>
Sign up to request clarification or add additional context in comments.

1 Comment

@leora Sorry I had just missed off the conditional, try the above.
1

You may be tied to your solution of using HiddenConditional in your ViewModel, but I think it would be better to keep the class name in your View and simply use a boolean in your ViewModel. So it would look like this:

ViewModel

public bool MyListIsHidden { get; set; }

View

<%= Html.DropDownListFor(r=>r.Id, Model.MyList, new { @class = "Dropdown" + (Model.MyListIsHidden ? " hidden" : "") })%>

Comments

Your Answer

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.