I want to display data in a viw after parsing json data from controller and also save it in database through linq query, I have created model, view and controller but can't get the data in a view, and it is not saved in database, i have take a list of type EmployeeModel and if id matches added record to that list and then want to store that record in database and send it to view. I am getting error in view that foreach does not implement ienumerator. Please help to sort out this problem thanks......
namespace jsonMvcApplication.Models
{
public class jsonData
{
public List<rootelem> data { get; set; }
}
public class rootelem
{
public List<employeeObj> employee { get; set; }
}
public class employeeObj
{
public List<DataElement> empdetails { get; set; }
}
public class DataElement
{
public ulong empid { get; set; }
public string empname { get; set; }
public string empdept { get; set; }
public List<empphone> empphone { get; set; }
}
public class empphone
{
public string home { get; set; }
public string mobile { get; set; }
}
}
-Controller
public ActionResult Index()
{
var empid=111;
WebClient c = new WebClient();
EmployeeModel empClass;
var Emplist=new List<EmployeeModel>();
var jsonstring = @"{""data"":[{""employee"":[{""empdetails"":[{""empid"":""98977"",""empname"":""John"",""empdept"":""HR"",""empphone"":[{""home"":""868685768"",""mobile"":""89886654""}] }] }] }] }";
empClass = JsonConvert.DeserializeObject<jsonData>(jsonstring);
foreach (var items in empClass.data[0].employee[0].empdetails)
{
if (empid==111)
{
Emplist.Add(empClass);
}
}
return view(Emplist);
}
- View
@model jsonMvcApplication.Models.DataElement
<table>
@foreach(var empdetails in Model)
{
<tr> <td> @empdetails.empid </td> </tr>
<tr> <td> @empdetails.empname </td> </tr>
}
</table>
myData?JsonConvert.DeserializeObject<empClass>(jsonstring);is not correct.empClassis not a Type