0

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> 
1
  • How about posting a compilable code? What is myData? JsonConvert.DeserializeObject<empClass>(jsonstring); is not correct. empClass is not a Type Commented Sep 25, 2013 at 17:20

2 Answers 2

0

your model definition

@model jsonMvcApplication.Models.DataElement

seems to not match with what you return in your controller right? Emplist <> DataElement

Sign up to request clarification or add additional context in comments.

Comments

0

Try below in your view

<script>
var values = @Html.Raw(Json.Encode(Model.Values));
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.