Tuesday, August 16, 2011

How to use a collection in MVC which will populate the ViewModel correctly


The view model below is a collection of strings called Images


public class PostItemViewModel
{
   public PostItemViewModel()
   {
      Images = new List<string>();
   }

   public List<string> Images { get; set; }
}


The View would look something like the code below.  The reason for the HiddenFor, is so that when  you post back to your controller, the view model that is posted back will contain the Image list.


<table>
   <tr>
   <%
   for (int i = 0; i < Model.Images.Count; i++)
   {
   %>
      <td>
         <%= Html.HiddenFor(m => m.Images[i]) %>
         <img alt="<%= Model.Images[i] %>" width="50" src="\Uploads\<%= Model.Images[i] %>" />
      </td>
   </tr>
</table>

Tuesday, April 12, 2011

How to show differences in two files using tortoise SVN

Right click on the file > Tortoise SVN > Show log

Click on the first version of the file

[Ctrl] Click on the second file

Right click > Compare Revisions

Thursday, March 17, 2011

How to use multiple login pages in MVC

When you only have one login page, then it's easy to add the authorize attribute to your action and you'll automatically be directed to the login page in your web.config


[Authorize(Roles = "Administrator")]
public ActionResult EditCategory(int id)
{
   return View();
}
On my sites, I have a separate login page for the Administrative area, so I needed to create a custom attribute.

[CustomAuthorization(LoginPage = "~/Admin/LogOn", Role = "Administrator")]
public ActionResult EditCategory(int id)
{
   return View();
}
/// <summary>
/// Allows you to specify the Login Page and Role with an attribute:  [CustomAuthorization(LoginPage="~/Admin/LogOn",Role="Administrator")]
/// </summary>
/// <param name="filterContext"></param>
public class CustomAuthorization : AuthorizeAttribute
{
   public string LoginPage { getset; }
   public string Role { getset; }
 
   public override void OnAuthorization(AuthorizationContext filterContext)
   {
      //Send the return url to the login page
      LoginPage += "?ReturnUrl=" + filterContext.HttpContext.Request.RawUrl;
      if (!filterContext.HttpContext.User.Identity.IsAuthenticated)       {          filterContext.HttpContext.Response.Redirect(LoginPage);       }       else       {          if (Role != null)          {             if(!filterContext.HttpContext.User.IsInRole(Role))                filterContext.HttpContext.Response.Redirect(LoginPage);          }       }       base.OnAuthorization(filterContext);    } }

Wednesday, March 9, 2011

How to calculate age in Excel

This formula will calculate a person's age in excel (assuming date of birth is in cell A1)


=IF(MONTH(TODAY())>MONTH(A1),YEAR(TODAY())-YEAR(A1),
 IF(AND(MONTH(TODAY())=MONTH(A1),DAY(TODAY())>=DAY(A1)),
 YEAR(TODAY())-YEAR(A1),(YEAR(TODAY())-YEAR(A1))-1))

Thursday, February 24, 2011

How to change the title of a jquery dialog

Here is an image that a user can click to get help

   <img src="/Content/images/Help.png" style="cursor:pointer" onclick="javascript:ShowHelp('My Title')" />

Here is the jquery dialog

   <div id="dialog-help" title="" style="background:white">
      <div id="divHelpContents"></div>
   </div>

Javascript function to set up the dialog

      $(document).ready(function ()
      {
         //Help popup
         $("#dialog-help").dialog({ autoOpen: false
            , modal: true
            , width: 415
            , height: 300
            , show: "blind" //animation effect
            , hide: "clip" //animation effect            
            , draggable: true
            , resizable: false
            , buttons: {
               Ok: function ()
               {
                  $(this).dialog("close");
               }
            }
         });
      });
Here is a function that will update the title, help contents, and open the dialog
      function ShowHelp(topic)
      {
            $("#dialog-help").dialog('option', 'title', topic);
            $("#divHelpContents").html("Show some help contents here");
            $("#dialog-help").dialog("open");
      }

Friday, February 18, 2011

How to get paste to work with Remote Desktop Connection

You can copy things to the clipboard and paste them into your remote desktop connection.

What do you do when paste stops working ?

1.  Close your remote desktop connection (you don't need to log out).

2.  Reconnect.

How to re-insert deleted records when there is an identity field

My customers like to delete important records and then have me restore them from a backup when they realize their mistake.

When the column has an identity field, and you want to restore the records with the same number as they had before deleted, you must turn on identity insert with this TSql command

SET IDENTITY_INSERT {YourTableName} ON 

insert the deleted records

SET IDENTITY_INSERT {YourTableName} OFF