
This is a RouteHandler method C# Controller to JS !
In this post I'II show you how you can use Jquery to call MVC Action methods which produce Json data. Json data can be just as effective as actual weapon.Firstly you need JsAction.Writing JsAction attributes both js handshake file and add attribute Csharp Controller side. But you would like to use jsAction in big Project.When writing HANDSHAKE js file (script.js) manually, You will afford more time.i've been decided to use T4 template.Using this template is easy. You just need to apply the right style T4 coding which is depended on the T4 context. The article should be organized into multiple sections. Lets see steps!
Developing T4 Template
- Write access code Nortwind db( I prefer NorthWind)
- Design js file creater code
- Press Ctrl+S to generate Js file
- Copy paste other js files.
You must produce Js handshake file in big project. I think it is useful to reduce time expenses. I would like to generate a js file that is a kind of handshake template JsAction is using this js file to communicate MVC Controller's Actions.Let's talk about code. Code snippets are formatted like below:
- <#@ template language="C#" Debug="true" #>
- <#@ output extension=".js" #>
- <#@ assembly name="System.Xml"#>
- <#@ assembly name="Microsoft.SqlServer.Smo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" #>
- <#@ assembly name="Microsoft.SqlServer.ConnectionInfo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" #>
- <#@ assembly name="Microsoft.SqlServer.Management.Sdk.Sfc, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" #>
- <#@ import namespace="Microsoft.SqlServer.Management.Smo" #>
- <#@ import namespace="System.Collections.Generic" #>
- <# Dictionary <string,string> DataTypes=new Dictionary<string,string>()
- {
- {"varbinary","Byte[]"},
- {"binary","Byte[]"},
- {"text","String"},
- {"bit","boolean"},
- {"smallint","Byte"},
- {"int","int"},
- {"bigint","long"},
- {"nvarchar","String"},
- {"datetime","DateTime"},
- {"decimal","decimal"},
- {"char","string"},
- {"varchar","string"},
- {"nchar","string"},
- {"numeric","decimal"},
- {"image","byte[]"},
- {"ntext","string"},
- {"money","decimal"},
- {"real","decimal"}
- };
- Server srv = new Server("PC");
- srv.ConnectionContext.LoginSecure = false;
- srv.ConnectionContext.Login = "sa";
- srv.ConnectionContext.Password = "@xxxx";
- Database database = new Database(srv,"NORTHWND");
- database.Refresh();
- int i =0;
- foreach (Table tab in database.Tables)
- {
- if (tab.Name!="sysdiagrams")
- {#>
- JsActions.<#=tab.Name.ToString()#> ={
- <# foreach (Column col in tab.Columns)
- { i++; #>
- <#= tab.Name.ToString()#>By<#=col.Name#>: function (<#=col.Name#>) {///<param name="<#=col.Name#>" type = "<#=DataTypes[col.DataType.ToString()]#>"></param>
- }<# if(i<tab.Columns.Count){ #> , <#} else { i=0;} #>
- <# }#>
- }<# }}#>




Yusuf KaratoprakPosted Jun 6, 2013, 10:59 AM
Thanks Dinesh.Time is big problem for me. I have few article ideas. i would like to wirte new one...
Dinesh BeniwalPosted Jun 6, 2013, 10:53 AM
Welcome Back.