0

I want to insert a condition in my view. The condition is based on information from the database. What is the best way to do this? I considered inserting that value into ViewBag in controller or creating an additional property in the view model, but they are dirty ways and I would prefer to avoid them.

Thanks in advance.

pt12lol
  • 2,332
  • 1
  • 22
  • 48
  • Can you give a more detailed definition of what 'condition' you want – Daniel Dawes Sep 25 '13 at 09:01
  • @Daniel Dawes: I think the kind of condition does not matter. I need some info from db and construct the condition based on them. – pt12lol Sep 25 '13 at 09:05
  • @pt12lol The reason I ask is condition could mean anything, from 'if this... then that...' to 'show this template if status is that, or throw this error if message is this...' etc. – Daniel Dawes Sep 25 '13 at 09:07
  • @Daniel Dawes: I see. I want to enable or disable for edit some list items if condition is true or false. – pt12lol Sep 25 '13 at 09:09
  • Bind to a ViewModel and then you can use razor syntax to do something like @if (condition){ true }else{false} – Daniel Dawes Sep 25 '13 at 09:11

1 Answers1

1

Create a ViewModel(it represents the data that you want to display on the page), if you think the property does not fit in your model.

I am assuming you would be using the property to hide and show some elements.

See this SO answer on various ways to send extra information to view

Community
  • 1
  • 1
Anand
  • 14,545
  • 8
  • 32
  • 44
  • @pt12lol It does sound like you should be using a ViewModel as shown above, and binding any conditions you might have to that model. See here: http://stackoverflow.com/questions/16548376/asp-net-mvc-how-exactly-to-use-view-models – Daniel Dawes Sep 25 '13 at 09:08
  • I see. But as I mentioned in my question, I would prefer to avoid that way. Do you think it is the best way to do this? I am sorry I didn't precise what I understand under `model` word. – pt12lol Sep 25 '13 at 09:18