Jquery json ajax mvc

不说什么 直接上代码

前台:

function percent(){
$.getJSON('<%= Url.Action("Getpercent", "Main")%>?id='+Math.random() , function(data) {
if (data.Result == 'EXCEPTION') {
alert(data.Data);
}
if (data.Result == 'SUCCESS') {
document.getElementById("bUid1").innerText=data.Data;
//document.getElementById("Bt").style.backgroundColor=data.Color;
document.getElementById("Bt").style.backgroundImage=ReturnColor(data.Color);
document.getElementById("BtnStop").style.backgroundImage=data.ColorStop;
document.getElementById("lable1").innerText=data.DTime;
}

})
};

注:<%= Url.Action("Getpercent", "Main")%> 调用main  control里面的 Getpercent 方法


后台代码:

control里面的方法

public ActionResult Getpercent()
{
string tt = GetWarn()----string类型;
return JsonResultGet(new JsonModel<string>(JResult.SUCCESS, tt, color, colorstop, DateTime.Now.ToString("yyyy/MM/dd HH:mm")));
}
protected ActionResult JsonResultGet<T>(JsonModel<T> jmodel)
{
return Json(jmodel, JsonRequestBehavior.AllowGet);

}

jsonmodel:

public class JsonModel<T>
{
public T Data { get; set; }
public string Result { get; set; }
public string Color { get; set; }
public string ColorStop { get; set; }
public string DTime { get; set; }
public JsonModel(string result, T data,string color,string colorstop,string dtime)
{
Result = result;
Data = data;
Color = color;
ColorStop = colorstop;
DTime = dtime;
}
}
public class JResult
{
public const string NOTFOUND = "NOTFOUND";
public const string EXCEPTION = "EXCEPTION";
public const string SUCCESS = "SUCCESS";
public const string ERROR = "ERROR";
public const string INFO = "INFO";
public const string AUTHENTICATED = "AUTHENTICATED";
}

总体方法:

先将数据用json模式处理,然后传到前台

posted @ 2011-07-13 12:37  naariah  阅读(336)  评论(0)    收藏  举报