0

I need to add foreach loop and add model data instead of this hardcoded data. I don't know how to add it inside javascript code with jQuery.

 @model  IEnumerable<HR.Models.Officials>


   @foreach (var item in Model)
        {




                            @Html.DisplayFor(modelItem => item.Id),
                            @Html.DisplayFor(modelItem => item.parentId),
@Html.DisplayFor(modelItem => item.Name)
    }
<script type="text/javascript">
        var peopleElement = document.getElementById("people");
        var orgChart = new getOrgChart(peopleElement, {
            primaryFields: ["name", "title", "phone", "mail"],
            photoFields: ["image"],
            dataSource: [
                { id: 1, parentId: null, name: "Amber McKenzie", title: "CEO", phone: "678-772-470", mail: "[email protected]", adress: "Atlanta, GA 30303", image: "images/f-11.jpg" } ]
        });
    </script>

I need to add a foreach loop with my model and add model instead of this hardcoded data. For example id:1 = id:@model.id. I need it in the foreach loop. Kindly need someone to guide me

8
  • If this is a view then just pull the data from the db in the controller method and load it using razor. Commented Feb 12, 2018 at 20:31
  • but how i can load data inside jquery script? can i use foreach loop inside dataSource: [...? Commented Feb 12, 2018 at 20:39
  • You could do it inside or define dataSource:[] and then in a razor for each loop do dataSource.push({...}). Commented Feb 12, 2018 at 20:43
  • Note, it is a razor foreach loop NOT a javascript for loop. stackoverflow.com/questions/11261590/mvc-razor-foreach Commented Feb 12, 2018 at 20:45
  • can you give me little practical example how i can use it? Commented Feb 12, 2018 at 20:50

2 Answers 2

1

This is one option:

@model  IEnumerable<HR.Models.Officials>


@foreach (var item in Model)
{
    @Html.DisplayFor(modelItem => item.Id),
    @Html.DisplayFor(modelItem => item.parentId),
    @Html.DisplayFor(modelItem => item.Name)
}
<script type="text/javascript">
    var peopleElement = document.getElementById("people");
    var orgChart = new getOrgChart(peopleElement, {
        primaryFields: ["name", "title", "phone", "mail"],
        photoFields: ["image"],
        dataSource: [
            @for(var i = 0; i < Model.Count; i++)
            {
                <text>
                    { id: @(Model[i].Id), name: "@(Model[i].name)", parentId: @(Model[i].parentId) }@(i < Model.Count - 1 ? "," : "")
                </text>
            }
        ]
    });
</script>

Note: If the value coming in needs to be a string in the javascript then you need to wrap quotes around it, like with "@(Model[i].name)". Because these are in the declaration of the array, the objects need a , between them which is why I made it a for loop instead of a foreach (though it could be done with a foreach as well).

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

4 Comments

thanks i will implement this code, if i get any problem i will get back to you
I just noticed that I was missing a closing paren after parentId. Try now.
@model IEnumerable<HR.Models.Official> <script type="text/javascript"> var peopleElement = document.getElementById("people"); var orgChart = new getOrgChart(peopleElement, { primaryFields: ["name", "title", "phone", "mail"], photoFields: ["image"], dataSource: [ @foreach (var item in Model) { <text> {id: @(item.EmpId), name: "@(item.Name)", parentId: @(item.Id) } </text> } ] }); </script>
You are missing the commas between the elements of the array. If you inspect the page you'll see the javascript error. I used @(i < Model.Count - 1 ? "," : "") to put the commas in.
0

Create something like this in a static class:

public static IHtmlContent ToJS(this IHtmlHelper htmlHelper, object obj)
{
    return htmlHelper.Raw(JsonConvert.SerializeObject(obj));
}

In your "_ViewImports.cshtml" also you can import System.Linq if needed.

Now you can simply do this:

<script type = "text/javascript" >
    var peopleElement = document.getElementById("people");
    var orgChart = new getOrgChart(peopleElement, {
        primaryFields: ["name", "title", "phone", "mail"],
        photoFields: ["image"],
        dataSource: @Html.ToJS(Model);
</script>

If you want to customize your array object, you can import Linq mentioned above and set dataSource this way:

dataSource: @Html.ToJS(Model.Select(m => new { m.id, m.name, m.parentId, ... }));

2 Comments

where this static class i should put?
For convince put it directly inside the web project (no folder). Class can have any name

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.