October 26, 2011

October 24, 2011

MVC3 - Chart

It will return back a JPEG image.

- In controller:
public ActionResult KennelChart(){
return View();
}


- In view to call Chart to display:
<img src="KennelChart" />

- In view to generate Chart:
@{ var chart = new Chart(width: 500, height: 300)
.AddTitle("Kennel Size")
.addSeries(
name: "Dogs",
xValue: new[] {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"},
yValue: new[] {"28", "31", "34", "28", "36", "28", "9", "30", "30", "28", "28", "29"}
).Write();}

Grid in MVC3

MVC3 include a WebGrid which you can bind your data into it and it has pagination and sorted column built-in.

@{ var grid = new WebGrid(soruce: Model, defaultSort: "Name", rowsPerPage: 4);}

@grid.GetHtml(columns:grid.Columns(
grid.Column("", format: item => Html.ActionLink("Details", "Details", new { id=item.ID})),
grid.Column("ID"),
grid.Column("Name"),
grid.Column("Gender"),
grid.Column("Hash", format: item => Crypto.Hash(item.Name)),
grid.Column("Neutered", format: item => item.Neutered?"Yes": "No"),
grid.Column("Spayed", format: item => item.Neutered?"Yes":"No"))
)

October 22, 2011

MVC - Ajax

- ActionLink:
On view -> Ajax.ActionLink(text, action method, RoutData, AjaxOptions{UpdateTargetId="DivResult"})
On Controller -> action method which return string or view


- BeginForm:
On View -> using(Ajax.BeginForm( action method, AjaxOptions{UpdateTarget="DivResult"})
On Controller -> [HttpPost] return string or view

-----------------------------------
function(int? id) // id can be null

- In View:
$.getJSON("/Dog/GetDogListJSON", null, function (data) { showData(data);});
function showData(data){
$.each(data, function (){
var list += this.Name, this.Age;});
$("#DisplayResult").replaceWith(list);
}


- In Controller:
return Json( dogs, JsonRequestBehavior.AllowGet);
-----------------------------------
Invoke JavaScript within Ajax

- In View:
<%: Ajax.ActionLink("some text", action method, null);>

- In Controller:
return JavaScript(@"Alert('Hi there!');");
return JavaScript("$('#DisplayResult').html('That's me!');");

October 12, 2011

MVC 2.0

<%@ MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Controllers.Models.Dog>" %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%= Html.LabelFor(model => model.ID) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.ID) %>
<%= Html.ValidationMessageFor(model => model.ID) %>
</div>
</fieldset>
------------------------
<%: Html.ActionLink("Get Author", "GetAuthor","Home")%>
<%: Ajax.ActionLink("Run JavaScript", "InsertJS", "Home", null) %>
<%: Ajax.ActionLink("Update Page", "UpdatePage", "Home", null) %>

October 06, 2011

Lambda and Delegation

delegate void ConcatString(string str1, string str2);

ConcatString concat;
Action concat;

if(Environment.GetcommandLineArgs().Length > 1)
- concat = WriteToFile;
- concat = delegate(string s1, string s2){WriteToFile(s1, s2); };
- concat = (s1, s2) => WriteToFile(s1, s2);
else
- concat = WriteToConsole;
- concat = delegate(string s1, string s2){WriteToConsole(s1, s2); };
- concat = (s1, s2) => WriteToConsole(s1, s2);

concat(mes1, mes2);
-------------------
private static void WriteToConsole(string str1, string str2){
Console.WriteLine("{0}\n{1}", str1, str2);}

private static void WriteToFile(string str1, string str2){
StreamWriter writer = null;
try{
writer = new StreamWriter(Environment.GetCommandLineArgs()[1], false);
writer.WriteLine("{0}\n{1}", str1, str2);
}catch{
Console.WriteLine("File write operation failed..");
}finally{
if(writer != null) writer.Close();
}
}

October 01, 2011

CSS - margin auto

This box is horizontally centered by setting its right and left margin widths to "auto". This is the preferred way to accomplish horizontal centering with CSS, and works very well in most browsers with CSS2 support.
#Content {
width:500px;
margin:0px auto;
text-align:left;
padding:15px;
border:1px dashed #333;
background-color:#eee;
font-family: Calibri, "HelveticaNeue", "Helvetica Neue", Helvetica, Arial, sans-serif
}