开发者

ASP.NET MVC - Hiding a panel when model is not available

开发者 https://www.devze.com 2023-01-27 13:43 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消