I am using C# with MVC. I want to set the div visible true/false based on a condition from database in the Get method of Controller.
Please suggest
I am using C# with MVC. I want to set the div visible true/false based on a condition from database in the Get method of Controller.
Please suggest
myDiv.Style["display"] = 'none';
or
myDiv.Visible = false;
Is this what you want ?
send the result from the database as part of the View Model
then you can use this syntax
<% if(Model.Property) == "desired value"{%>
<% RenderPartial("div")%>
<%}%>
the best approach would be to change the CSS property of the div using jQuery analysing the database value
$(function(){ if(<%Model.Property == "desired value"%>) $(div).hide(); });
In Controller:
ViewData["DivIsVisible"] = ...
return View();
// or with ViewModel
public class TheViewModel
{
public bool DivIsVisible;
...
}
...
var model = new TheViewModel { DivIsVisible = true /* false */, ... }
return View(model);
In View:
<script runat="server">
protected bool DivIsVisible {
get {
return ViewData["DivIsVisible"] != null && (bool)ViewData["DivIsVisible"];
}
}
</script>
<div <%= DivIsVisible ? "" : "style='display: none'" %>>
</div>
<% if(DivIsVisible) { %>
<div>
...
</div>
<% } %>
<!--or with View Model -->
<div <%= Model.DivIsVisible ? "" : "style='display: none'" %>>
</div>
<% if(Model.DivIsVisible) { %>
<div>
...
</div>
<% } %>