2

I have the following line in my HTML code :

<input type="hidden" value="@Model.BgtArray" id="BudgetArray"/>

Here, BgtArray is a 2-D array which gets populated in the view itself. Something like this:

@for(i=0;i<10;i++)
{@for (j=0;j<10;j++)
   @Model.BgtArray[i][j]= *some value*;
}

How do i access this array's elements in a jQuery function??

2 Answers 2

3

You are populating the model inside the view???? In the MVC pattern views are intended to display data that is passed to them under the form of a model. It is the controller's responsibility to populate this model.

This being said let's suppose that you have a model:

public class MyViewModel
{
    public int[][] BgtArray { get; set; }
}

which is populated inside the controller action and passed to the view:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            BgtArray = Enumerable
                .Range(1, 10)
                .Select(
                    i => Enumerable
                            .Range(1, 10)
                            .Select(j => i * j)
                            .ToArray()
                )
                .ToArray()
        };
        return View(model);
    }
}

and then we could have a corresponding strongly typed view in which we could JSON serialize the model and access it in javascript:

@model MyViewModel
<script type="text/javascript">
    var model = @Html.Raw(Json.Encode(Model));
    alert(model.BgtArray[1][2]);
</script>

which will be rendered in the browser like this:

<script type="text/javascript">
    var model = {"BgtArray":[[1,2,3],[2,4,6],[3,6,9]]};
    alert(model.BgtArray[1][2]);
</script>

As far as the following line is concerned:

<input type="hidden" value="@Model.BgtArray" id="BudgetArray"/>

it's pretty useless because you cannot store entire complex object graphs into a hidden field. The rendered result will be:

<input type="hidden" value="System.Int32[][]" id="BudgetArray"/>

which is very unlikely to be what you are trying to achieve.

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

2 Comments

Hey Darin. Thanks a ton for you response. however, im getting the following error on running your code: A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'. its occuring in this line: var model = @Html.Raw(Json.Encode(Model));
That's because you have circular references. You cannot JSON serialize object graphs with circular references. I would recommend you to use a view model instead of passing your domain models to the views and break this circular reference.
1

To solve the problem with circular references (mentioned in the comments below the answer for the original question), I used this instead of Json.Encode:

@Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model))

In your classes, use the following annotation to make Json ignore the navigation properties that cause trouble:

[JsonIgnore]
public virtual Item Item { get; set; }

Solution taken from: Hide field in model when using @Json.Encode

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.