MVC Action直接在網頁上回傳Class to Json資料
MVC Action直接在網頁上回傳Class to Json資料
[HttpGet]
public JsonResult SampleAction()
{
UserData udata = new UserData();
using (ssEntities ssDBContext = new ssEntities())
{
var query = from t1 in ssDBContext.UserData
select t1;
udata = query.FirstOrDefault();
}
/*
回傳是一個字串, 內容都是文字
"{\"PKID\":1,\"UserName\":\"彼得\",\"UserName_Eng\":\"Peter\",\"Sex\":\"Male\",\"Birthday\":\"1980-04-04T00:00:00\",\"MobilePhoneNo\":\"0968123123\",\"Interest\":\"Movie\",\"DeleteDate\":null,\"DeleteUserID\":null,\"DeleteUserName\":null,\"InterestText\":null,\"BirthdayString\":null}"
*/
string json = JsonConvert.SerializeObject(udata);
return Json(json, JsonRequestBehavior.AllowGet);
/*
回傳是Json格式, 沒有字串的雙引號包起來
{
"PKID": 1,
"UserName": "彼得",
"UserName_Eng": "Peter",
"Sex": "Male",
"Birthday": "/Date(323625600000)/",
"MobilePhoneNo": "0968123123",
"Interest": "Movie",
"DeleteDate": null,
"DeleteUserID": null,
"DeleteUserName": null,
"InterestText": null,
"BirthdayString": null
}
*/
//return Json(udata, JsonRequestBehavior.AllowGet);
}
public class User
{
public int PKID { get; set; }
public string UserName { get; set; }
public string UserName_Eng { get; set; }
public string Sex { get; set; }
public DateTime Birthday { get; set; }
public string MobilePhoneNo { get; set; }
public string Interest { get; set; }
public DateTime? DeleteDate { get; set; }
public int? DeleteUserID { get; set; }
public string DeleteUserName { get; set; }
public string InterestText { get; set; }
public string BirthdayString { get; set; }
}
留言
張貼留言