Monday, May 28, 2012

How to give Style for Html helper CompleteEditorFor ?

MVC View

<%: Html.CompleteEditorFor(m => m.Quantity, labelOverride: "Quantity In")%>

Style 

<style type="text/css">

        #Quantity
        {
            width: 100px !important;
        }

    </style>

Note: Be aware that Auto generated id value for html controller is Case Sensitive (ie: Quantity)
           Inside the style I have used !important property for override any existing width property
           inherit from other classes.

Friday, May 25, 2012

How to give custom Date format for null type?

Say you have model like below.

Model

public DateTime? date { get; set; }

Then you can use u'r View as below.

View

<%: date.HasValue ? date.Value.ToString("yyyy-MM-dd hh:mm:ss") : "[N/A]"%>

How to work with C# null Boolean Type ?

Say you have a Model like below

Model

public bool? IsAllowInvoice { get; set; }

At this point when your using above model inside a MVC Controller or View you must adhere below mentioned checking to Avoid Awful Results.

Good Practices

if (IsAllowInvoice.HasValue && !IsAllowInvoice.Value)  = true // Then IsAllowInvoice is false


if (IsAllowInvoice.HasValue && IsAllowInvoice.Value) = true //Then IsAllowInvoice is true


if (!IsAllowInvoice.HasValue)  = true //Then IsAllowInvoice is null


Important Point :

When you use above method You always need to check HasValue before
check the Value (like above).Otherwise It will give InvalidOperationException.

P.S. 1
In detail Answer for Comment Posted by Anonymous October 11, 2012 5:44 PM Developer.

I have mentioned above type of solution,Because which we can use if statements
without giving true / false or null as equal parameters.
Please look at below mentioned Scenario.

Scenario :

I want to hide Invoice radio button when IsAllowInvoice is false.
In other words Show Invoice radio button when IsAllowInvoice is true.


<%  if (Model.IsAllowInvoice.HasValue && Model.IsAllowInvoice.Value)  { %>
           <li id="invoiceService">
                   <label for="invoice">  Invoice </label>
                    <input type="radio" id="invoice" name="type" value="invoice" />
         </li>
 <%%>


P.S. 2   Mukesh Selvaraj gave a very good simple solution for null boolean handling.
            That is we can use below mentioned syntax for that.Thanks for Mukesh
           (for more details  please see comment section).


<%  if (Convert.ToBoolean(IsAllowInvoice))  { %>
           <li id="invoiceService">
                   <label for="invoice">  Invoice </label>
                    <input type="radio" id="invoice" name="type" value="invoice" />
         </li>
 <% } %>


Happy Coding.

May Be Use Full To You :
How to Improve Performance of Entity Framework Query (over 400 times)?