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.

Arunprasanth K V
  • 20,733
  • 8
  • 41
  • 71

1 Answers1

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>