This is an update to my previous post regarding conditional rendering partial views, in that I used the internal implementation of the Html.RenderPartail(…) method to create the Html extension. Later I found a simple way to achieve the same using Action<T> delegate
<p>Partial rendering with Action Methods</p>
<% Html.PartialIf(this.Model.Exists, html => html.RenderPartial("MyPartialView")); %>
If you look at the “PartialIf” implementation, it is simple, cleaner than the previous technique I have mentioned in my post.
public static void PartialIf(this HtmlHelper htmlHelper, bool condition, Action<HtmlHelper> action)
{
if (condition)
{
action.Invoke(htmlHelper);
}
}
That’s it
Update : Later I found a cleaner and simple approach to do the same – read this post ASP.Net MVC – Conditional rendering Partial Views with Action<T> delegate
Following my previous post about Conditional Rendering, one of my colleague asked me how to render the partial view based on a condition.
Normal way of doing this is
<p>Normal partial rendering based on condition</p>
<% if(this.Model.Exists)
{
Html.RenderPartial("MyPartialView");
} %>
I am not sure about any other technique for rendering partial view conditionally other than this (correct me if I am wrong
).
Then I thought about copying the pattern I have used in my previous post and came up with this code which could conditionally render partial views and you could use the Html extension like below, which more clean than the previous
<% Html.PartialIf(this.Model.Exists, "MyPartialView"); %>
We come across situations like rendering elements based on the conditions in Model. For example, in the view if we want to show a TextBox if some property is true. A normal way of doing this is like below
<% if (this.Model.Exists)
{%>
<%= Html.TextBox("Test") %>
<% }
This looks like old classic asp style, and when it comes to code maintenance this will be a pain. So a clean way is to use an Html helper to generate this
<%= Html.If(this.Model.Exists, action => action.TextBox("Name")) %>
which looks cleaner than the old one. Source code for this helper method is
public static string If(this HtmlHelper htmlHelper, bool condition, Func<HtmlHelper, string> action)
{
if (condition)
{
return action.Invoke(htmlHelper);
}
return string.Empty;
}
What about IfElse condition, we could write another helper method for that
I got this exception when I tried to run my Repository tests and couldn’t find the exact reason why these tests failed.
All the unit tests where passing before I removed the default constructors from Domain Model, when I reverted the default constructors back, everything started working again….. !!!
Do you know the exact reason for this?
Have you ever thought about how chatting in Gmail works? I think it works using a programming technique called “Comet”.
What is comet programming
In web development, Comet is a neologism to describe a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it. Comet is an umbrella term for multiple techniques for achieving this interaction. All these methods rely on features included by default in browsers, such as JavaScript, rather than on non-default plugins – – Wikipedia
Last few days, I was reading about this technology and thought about sharing the information with you all. Please find the attached sample application which demonstrates how comet works using Asp.Net (Note: Right now it works only in firefox, fixes or patches to make it work in IE/Safari.. or all the browsers in this world are welcome J )
Please download it from here – http://www.rajeeshcv.com/download/comet.zip