ASP.NET MVC - Hiding a panel when model is not available
I have the standard search scenario: user enters search parameters and clicks the search button - results show up in the panel below. What I want to do is to control the visibility of the panel based on whether search results are available or not. I have tried to code my panel as follows, but I am getting a parse error at run time.
<asp:Panel ID="ResponsePanel" Visible="<%= Model != null %>" runat="server">
...
</asp:Panel>
The parse error I am getting is this:
Cannot create an object of type 'System.Boolean' from its string representation
'<%= Model != null %>' for the 'Visible' proper开发者_开发知识库ty.
How do I toggle the panel depending on the availability of the model?
As I said in my comment, you should not be using a <asp:panel>
in your asp.net application.
Instead, build a partial view (ascx)
<% if(! Model.HasValue){ %>
<%: Html.Partial("WhatWouldGoInYourPanel") %>
<% } %>
Then in your Partial view, you can put all the "stuff" that you want to show if the Model is empty.
You can put the partial in one of two places. If it's shared, you put it in the Views/Shared
folder. If it's specific to the Controller, you put it in the Views/[ControllerName]
folder.
note: please forgive my C#... I'm not all that good.
精彩评论