Showing posts with label Sample Code. Show all posts
Showing posts with label Sample Code. Show all posts

Friday, 15 March 2013

LINQ Zip and join

Add two list sequentially together according to it order... stop when end of one list.

Fast but limited compare with join.

(roleList).Zip(roleDescriptionList, (r, rd) => new RoleDescription() { roleID = r.dataID, role = r.dataValue, description = rd.dataValue }).ToList();

roleList and roleDescriptionList are two separate lists.  They can be unrelated at all.
Using Zip() to join them together to create a new list of object "RoleDescription" one by one.

To have better control, you should use join instead.


IList<RoleDescription> result = (from r in roleList
                         join rd in roleDescriptionList
                         on r.dataID equals rd.dataID
                         select new RoleDescription { 
                             roleID = r.dataID, 
                             role = r.dataValue, 
                             description = rd.dataValue }).ToList();

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;"


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");
});

Monday, 4 February 2013

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);


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];