0

i have an action like below in my controller and the object of one Viewmodel class i had send as an argumnet to view. The problem is i need to get this object values in javascript.

public ActionResult FillChecklist()
{
 classOne objVM=new classone();
    objVM.List=//get list 
    objVM.Id=//someid
    objVM.List2=//secondlist
 return View(objVM);
}

i had tried something like below but it does not works. i know hidden variable assign is a solution but i don't know if model class has many lists then how can i get the list in javascript.

<script type="text/javascript>
var obj=@Model;

</script>

i had tried the below method too. but it shows the name json is not exist in this current context

var obj = JSON.parse('@Html.Raw(Json.Encode(Model))');

please help me to solve this issue.

1 Answer 1

1

I just ran a test for you with the following code:

@model Project.ViewModels.TestViewModel
@using System.Web.Helpers

<script type="text/javascript">
    var obj = JSON.parse('@Html.Raw(Json.Encode(Model))');
</script>

ViewModel:

public class TestViewModel
{
    public string Test { get; set; }
}

It produces the following output:

<script type="text/javascript">
    var obj = JSON.parse('{"Test":"Value123"}');
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

it shows one more warning conditional compilation is turned off
I edited my answer and ran a test for you. This solution works 100%
i can't write JSON.parse('@Html.Raw(Json.Encode(Model))'); it shows error like json does not exist in this corrent context that is my problem

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.