Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
169
rated 0 times [  173] [ 4]  / answers: 1 / hits: 5383  / 11 Years ago, thu, january 9, 2014, 12:00:00

In controller I have



 public JsonResult Index()
{
List<TutorialModel> models = new List<TutorialModel>();
model.TitleWord = Go;
model.Colors = new List<bool>();
model.Colors.Add(true);
model.Colors.Add(false);
model.Colors.Add(false);
model.Colors.Add(false);
model.PossibleAnswers = new List<string>();
model.PossibleAnswers.Add(1);
model.PossibleAnswers.Add(2);
model.PossibleAnswers.Add(3);
model.PossibleAnswers.Add(4);

string ser = (new System.Web.Script.Serialization.JavaScriptSerializer()).Serialize(model);

return Json(ser, JsonRequestBehavior.AllowGet);
}


And when In view I try to catch this Json result with



<script>
var values = json.parse(@Model)
</script>


And browser shows me my serialize string. How I can deserialize this json element and store in some variable.



My model for this Controller :



public class TutorialModel
{
public string TitleWord { get; set; }

public List<string> PossibleAnswers { get; set; }

public List<bool> Colors { get; set; }

}

More From » json

 Answers
15

Note that in your Controller, you don't need to return a JsonResult - unless you're calling it as an ajax method or something. In this case, try returning an ActionResult (or ViewResult) as normal. You could do something like this:



public ActionResult Index()
{
List<TutorialModel> models = new List<TutorialModel>();
//...add items to list

return View(models);
}


Then in your cshtml javascript:



<script>
var model = @(Html.Raw(Json.Encode(Model)));
<script>

[#48854] Wednesday, January 8, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alorac

Total Points: 262
Total Questions: 82
Total Answers: 97

Location: Libya
Member since Mon, Dec 7, 2020
4 Years ago
alorac questions
Sat, Oct 10, 20, 00:00, 4 Years ago
Tue, Sep 22, 20, 00:00, 4 Years ago
Wed, Jul 1, 20, 00:00, 4 Years ago
Wed, Jun 3, 20, 00:00, 4 Years ago
Sun, May 17, 20, 00:00, 4 Years ago
;