4

I am working on an MVC4 razor project. I have a list of role objects that I store on my model as a jsonResult intended to be used in my client-side javascript.

//Model
public JsonResult JsonRoles { get; set; }

//Controller
var myroles = from r in myroles select new { r.Id, r.Description };
var myModel.JsonRoles = Json(myroles);


//Client side javascript
var data = '@(Model.JsonRoles)';
alert(data);

I tried to access this in javascript as below. When I alert I always get the string "System.Web.Mvc.JsonResult" but what I need is the json data. What am I doing wrong? Can someone please point me in the right direction

3 Answers 3

6

I have used ViewData to solve your problem and im able to get the result on the similar lines you can resolve model property also

//Contoller Class 
 public ActionResult CreateRequest()
        {

            var data = new { Id = "one", Make = "Two" };


            ViewData["Data"] = Json(data);

            return View();

        }

//And client side is 

  <script type="text/javascript">
        var data = @Html.Raw(Json.Encode(ViewData["Data"]));
        alert(JSON.stringify(data.Data));
    </script>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Ajay. This solved my problem. I had tried Json.Encode and Html.Raw but not both together.
Good answer, saved me a lot of trouble. However this didn´t work for me unless I used explicit expression @(Html.Raw(Json.Encode(ViewBag.Stuff))); but maybe that´s due to some other aspects of my code. Thanks :)
0

JsonResult tends to be used as the return type of an action method you intend on calling from Ajax.

You need to use the JavaScriptSerializer to serialize your "myroles" object. E.g.

JavaScriptSerializer serializer = new JavaScriptSerializer();
myModel.JsonRoles = serializer.Serialize(myroles);

Change the type of JsonRoles on your model to string.

public string JsonRoles { get; set; }

Finally, output in your view as e.g.:

var data = @Html.Raw(Model.JsonRoles);
alert(data.id);

1 Comment

HI Tony. Thanks for the answer. I had already attempted this but I was getting escape characters in Json making it invalid. I could remove the escape characters but I was trying to get a better solution that would not require me to do this elsewhere in my project. I went with Ajay's answer below as it got around this problem.
0

In linqpad, I was able to get the data back out via

var x= new System.Web.Mvc.JsonResult();
x.Data=new System.Web.Script.Serialization.JavaScriptSerializer().Serialize("testing, testing");
x.Data.Dump();

also checked with

var x= new System.Web.Mvc.JsonResult();
x.Data=new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(new{test="testing"});
x.Data.Dump();

Which may not be the correct solution for in a razor page. I expect

var data = "@{Model.JsonRoles.ExecuteResult(this.ControllerContext);}";

to be correct in a proper mvc app.

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.