Last post Jan 28, 2010 03:44 PM by haefti
Jan 18, 2010 01:38 PM|haefti|LINK
As a newbie coming from a Java background with tag libraries I wonder if there is a simple possibility to render areas depending on backend values.
For example if I have search form I do not want to render the table for the search results as long as the user hasn't pressed the search button. Of course I can iterate over the model containing an empty list but this does not allow to suppress rendering
the whole table.
Using web forms I would just control the visibility of the affected tag(s) in the code behind file but how do I do this with ASP.NET MVC?
Can I use a partital view for a case like this? But I think I would have the same problem. How can I determine if a partial view is rendered (e.g. the whole result table with header)?
Thanks for any help!
Jan 18, 2010 02:23 PM|ignatandrei|LINK
You can do this in many ways:
1. put the table in with style = "display : none" and the through Jscript display:block ( or use jquery show $("#id_of_the_table").show();
2. load the data when you press the button with jscript by calling a method on the server and returning results (in JSon format , if you want) and putting on the table
Jan 18, 2010 04:13 PM|haefti|LINK
Thanks for your answer but actually I was more interested in doing this on the server side.
Obviously I can toggle the visibility via DOM but I would like to have some control structures to get certain output depending on backend properties.
I found examples like
<% } %>
which fulfill my needs.
Of course I don't want to put loads of C# code in the view but simple control structures are necessary to render not every element which is possibly needed.
Jan 22, 2010 01:04 AM|cvrajeesh|LINK
You could clean the code you have written much more clean way as described
In order to render the partial view conditionally try this technique explained
Jan 22, 2010 03:58 AM|krokonoster|LINK
Jan 22, 2010 08:04 AM|haefti|LINK
First of all thanks a lot for your very helpful answer!
Looking at the date of your blog postings I realize I'm developing at the bleeding edge.
Of course using inline code is not preferable but I did not know if a custom HtmlHelper is the only way to achieve the goal.
Coming from a background with predominantly free tag libraries I'm suprised that I have to write my own replacements for that in ASP.NET MVC but it's way better than using inline codes.
Are there any free custom HtmlHelpers available as a download package besides the ones integrated in MVC Futures?
If not, someome should start to collect and package those useful helpers to make them available for the community.
As you say: "Thanks to open source!"
Jan 25, 2010 05:14 AM|cvrajeesh|LINK
Glad that, it helped you. One of colleague also faced similar kind of problem few weeks back which made me to think about the HtmlHelper.
I am new to MVC, so not sure about any other techniques other than HtmlHelper for these purpose. May be "MVC futures" may come up with this type of helper method in the coming release. Till that time, we may have to write it our own.
Jan 28, 2010 03:44 PM|haefti|LINK
If it's possible to cover your custom helpers in a server tag you could get something similar to tag libraries or normal ASP tags in web forms which would allow well formed (X)HTML compatible views.
This would allow more simple work for frontend designers and even for developers.
I need to try if it's possible to pass all the parameters to the custom tag.