Wednesday 20 February 2013

MVC StringLength and maxlength

When you add the validation StringLength in MVC, it won't automatically limit the user to the max length.  You need to manually to add this in the View.

You cannot use @Html.EditorFor and you should use @Html.TextBoxFor.


<%=Html.TextBox("polNum",null, new {maxlength=10}) %>

@Html.TextBoxFor(model => model.homePostalCode, new { maxlength = 7 })

Wednesday 13 February 2013

MVC Razor Html.ActionLink new page

@Html.ActionLink("More info...", "../newpage", null, new { target = "_blank" })

Cannot put the target option in the 3rd parameter, it will treat it as "?target=_blank".

Tuesday 12 February 2013

jQuery UI Dailog, IE and DropDownList

There is a problem to select from a dropdownlist in a jQuery Dialog plug-in.  It happens only in IE (8 and 9, at least).  It works in FireFox.

  • The very first time (after opening the dialog) will work but it won't when I try to select another value
  • I can select the value using the keyboard
  • Tab to the dropdownlist and select with the mouse
  • I tired to add the z-index to the dropdownlist but it doesn't help
Solution:
  • The current solution is adding a mouseover event to set the focus to the dropdownlist.
onmouseover = "this.focus();return true;"


Relationship

“I’d rather do nothing with you than something without you.”

Monday 11 February 2013

Relationship

Relationships don’t survive because the guilty are punished but because the innocent are merciful!

Tuesday 5 February 2013

jQuery Template

pass a string:
$.tmpl("<div class="myClass">${FirstName},/div>", myObject).appendTo("#myDiv");

use a selector to find a script block on the page:
$("#myTemplate").tmpl(myObjects).appendTo("#myDiv");

...

<script id="myTemplate" type="text/x-jquery-tmpl">
    <tr>
        <td>${FirstName},/td>
        <td>${LastName},/td>
        <td>${Twitter},/td>
    </tr>
</script>

get an external "html" which contain the html template:

$.get("MyTemplate.htm", function (data) {
    $.tmpl(data, myObjects).appendTo("#myDiv");
});

jQuery General Performance Tips


  • use the latest jQuery
  • Caching Selections (assign it to a variable) 
  • Caching Other Items
  • Use element properties (DOM) when possible (instead of jQuery properties)


Monday 4 February 2013

jQuery Type Testing Functions

  • Determine the type of an object
  • Useful for optional parameters & validation

Functions:
  • $.isArray(array)
  • $.isFunction(function) - another way to do this ( if (typeof <functionName> == "function")
  • $.isEmptyObject(object)
  • $.isPlainObject(object)
  • $.isXmlDoc(doc)
  • $.isNumeric(number)
  • $.isWindow(window)
  • $.type(object)

 Return Values:
  • boolean
  • number
  • string
  • function
  • array
  • date
  • regexp
  • object
  • undefined
  • null

Turn javascript object into a string

use json2.js from http://www.JSON.org/json2.js

e.g.
<script src="json2.js"></script>
...
JSON.stringify( {
    FirstName: $('#FirstName').val(),
    LastName: $('#LastName').val()
});

To turn a JSON string into javascript ojbect, use parseJSON.

var json = '{"fname" : "john", "lname" : "smith", "age" : 35}';
var jsObject = $.parseJSON(json);


Fiddler

Download the free web debug tool here:
https://fiddler2.com/fiddler2/version.asp

When working on the localhost, add the "." before the port, e.g.

http://localhost.:1064/

WCF returning Json data

in the .net world, we need to get the data by getting the property d like this:

var cust = data.d[0];

var cust = data.d.firstname;

this won't work: var cust = data[0];