1

I have to write C# code for showing or hiding div in MVC3 for various controls based on switch case.How can it be done without using JQuery show or hide function? I want it fully server-side in the class or controller file? inline C# in cshtml page also not preferred?

3
  • 1
    Your cshtml can only dynamically control content if you "inline" code to determine what it displays. That's... just how it works. Why would you want to avoid that? You don't necessarily need the entire switch statement in your cshtml file though, you could set various properties in the ViewBag or ViewModel to determine what elements to display. Commented Mar 7, 2013 at 10:15
  • I don't see why you'd want to keep this code out of the cshtml page when a div is part of the view but...your call. You could fire a different view in the controller but this would be equally horrible from a design point of view. Commented Mar 7, 2013 at 10:18
  • possible duplicate of How to Show and Hide Div using C# in MVC 3 Razor View Engine? Commented Mar 11, 2013 at 16:53

1 Answer 1

2

I'd recommend that you put the conditional logic in the view model and expose them as boolean properties (or a list if applicable).

Then you could have simple statements in the Razor-views like:

@if (Model.ShowX) 
{ 
    // component html or
    // call to @Html.Partial() or @Html.Action()
}
Sign up to request clarification or add additional context in comments.

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.