我想在从控制器解析json数据后在viw中显示数据并通过linq查询将其保存在数据库中,我已经创建了模型、视图和控制器但无法在视图中获取数据,并且它没有保存在数据库中,我有一个 EmployeeModel 类型的列表,如果 id 匹配添加到该列表的记录,然后想将该记录存储在数据库中并将其发送到查看。鉴于 foreach 没有实现 ienumerator,我收到错误消息。请帮忙解决这个问题谢谢......
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; }
}
}
-控制器
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);
}
- 看法
@model jsonMvcApplication.Models.DataElement
<table>
@foreach(var empdetails in Model)
{
<tr> <td> @empdetails.empid </td> </tr>
<tr> <td> @empdetails.empname </td> </tr>
}
</table>