MVC / JSONP / DataSet Binding
I was able to get JSONP to work with MVC by modifying the code above. This sample directly binds datasets to html elements via JSONP.
Controller
>
public class HomeController : Controller
{
[HttpGet]
public ActionResult HeaderJSONP()
{
DsPromotion.HeaderDataTable tbl = new DsPromotion.HeaderDataTable();
DsPromotion.HeaderRow row = tbl.NewHeaderRow();
row.imgBanner_src = "/Content/Home/Image/MainBanner.gif";
tbl.Rows.Add(row);
return new JsonpResult { Data = tbl };
}
}
JSONP Result
> public class JsonpResult : System.Web.Mvc.JsonResult
> {
> public override void ExecuteResult(ControllerContext context)
> {
> this.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
> if (context == null)
> {
> throw new ArgumentNullException("context");
> }
>
> HttpResponseBase response = context.HttpContext.Response;
> if (!String.IsNullOrEmpty(ContentType))
> {
> response.ContentType = ContentType;
> }
> else
> {
> response.ContentType = "application/json";
> }
> if (ContentEncoding != null)
> {
> response.ContentEncoding = ContentEncoding;
> }
> if (Data != null)
> {
> HttpRequestBase request = context.HttpContext.Request;
> JavaScriptSerializer jsonserializer = new JavaScriptSerializer();
> DataTableConverter serializer = new DataTableConverter();
> response.Write(request.Params["jsoncallback"] + "(" + jsonserializer.Serialize(serializer.Serialize(Data, new JavaScriptSerializer())) + ")");
> }
> }
> }
Javascript / JQuery JSON Request and Callback
>
function BindDataTable(dataTable) {
var tableName;
for (tableName in dataTable) {
if (tableName.indexOf('') > 0) {
tableName = tableName.split('')[0];
}
}
var elementAndAttrib;
for (elementAndAttrib in dataTable[tableName][0]) {
var elementID = elementAndAttrib.split('')[0];
var attribName = elementAndAttrib.split('')[1];
var attribValue = dataTable[tableName][0][elementAndAttrib];
$("#" + elementID).attr(attribName, attribValue);
}
}
function GetHomeHeaderCallBack(tblHeader) {
BindDataTable(tblHeader);
}
function GetHomeHeader() {
var call = "/Home/HeaderJSONP?jsoncallback=?&" + Math.round(new Date().getTime());
$.getJSON(call, { format: "json" }, GetHomeHeaderCallBack);
}
$(GetHomeHeader);
Partial View
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
XSD
...
Table Serializer
>
public class DataTableConverter : JavaScriptConverter
{
public override IEnumerable SupportedTypes
{
get { return new Type[] { typeof(DataTable) }; }
}
public override object Deserialize(IDictionary<string, object> dictionary, Type type,
JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
DataTable listType = obj as DataTable;
if (listType != null)
{
// Create the representation.
Dictionary<string, object> result = new Dictionary<string, object>();
ArrayList itemsList = new ArrayList();
foreach (DataRow row in listType.Rows)
{
//Add each entry to the dictionary.
Dictionary<string, object> listDict = new Dictionary<string, object>();
foreach (DataColumn dc in listType.Columns)
{
listDict.Add(dc.ColumnName, row[dc.ColumnName].ToString());
}
itemsList.Add(listDict);
}
result[listType.TableName] = itemsList;
return result;
}
return new Dictionary<string, object>();
}
}
Enjoy!
Mark Brito