asp.net - Convert c# list of objects to JSON -
how parse c# object json?
this code got:
model:
namespace mvcapplication1.models { public class holidaymodel { public int holidayid { get; set; } public string holidaydescription { get; set; } public string holidaystartdate { get; set; } public string holidayenddate { get; set; } public int count { get; set; } public string holidayold { get; set; } public string holidaynew { get; set; } public int employeeid { get; set; } public int holidayselectedyear { get; set; } public int startday { get; set; } public int startmonth { get; set; } public int startyear { get; set; } public int endday { get; set; } public int endmonth { get; set; } public int endyear { get; set; } } }
controller:
public actionresult getevents() { list<holiday> holidays = conn.holidays.tolist(); viewbag.holidaysjson = json(holidays, jsonrequestbehavior.allowget); return view(); }
and in view try print json in alert box following code:
<script> alert("@viewbag.holidaysjson"); </script>
but empty alert box.
what's problem here?
you can pass model view , convert json using json.encode
method:
controller code :
public actionresult getevents() { list<holiday> holidays = conn.holidays.tolist(); return view(holidays); }
and in view:
@model list<holiday> <script> var holidaysjson = @html.raw(json.encode(model)); // json object alert(json.stringify(holidaysjson)); // converting string representation </script>
Comments
Post a Comment