Domanda

If I do something like :

public ActionResult RecruitingGoneAngular(int? projectId)
{
    projectId = projectId ?? 538;

    Project project = null;
    project = ProjectRepository.GetProjectById(projectId.Value, false, true, true, false);

    return View(Json(project));
}

How would I access the Json in the View?

Is there a way I can console log the Json?

I tried to do the typical @model Project at the top of the page. Then tried @Model.Name but apparently that doesn't work.

So how do I access the Json?

È stato utile?

Soluzione 2

System.Web.MvcController.Json returns an object of type System.Web.Mvc.JsonResult.

You are passing this object as a model to your view. So you should declare your model as

@model System.Web.Mvc.JsonResult

But this is not usefull.


If you want to serialize an object as JSON for use in your view, then use a serializer for that. The model will be a string then.

Some serializers you can use:

// action code in controller
string json = JsonConvert.SerializeObject(project)m
return this.View(json);
// in the view
<script>
var json = @Html.Raw(this.Model);
</script>

EDIT: you can also pass the project variable as the view's model. You will be able to use the project variable within the view and serialize it as JSON in there.


If you want to return JSON for an AJAX call, then

return this.Json(project);

You will have to rely on javascript to get the JSON with an AJAX request.

Altri suggerimenti

JSON is a data format, which gives a string representation to (possibly complex) objects, entities.

You are kind of misusing the different types of ActionResults.

The method Controller.Json method returns a JsonResult instance, which is a special ActionResult, representing that the response for the current HTTP request should be a the object serialized into JSON format. So no View at all should take part in this kind of response, but for example you could query such an Action from jQuery's ajax method, and process the JSON response on the client side. This tecnique is usually called Json WEB API: an Action which receives some parameters, processes them and return a "pure" result object, most likely serialized as JSON.

On the other hand, the Controller.View method returns a specific ViewResult, which represents that the response for the current request should be a full, rendered HTML piece. To this method you can pass an object as the model of the strongly-typed view. That object should be exactly of type which is defined in your view with the @model keyword, so mostly a .NET object, not a Json string, especially not a JsonResult object.

I hope I could make things a bit clearer.

UPDATE:

You cannot "send" the view and the Json at once. You can send Json response, or HTML response, but not both (at least technically not). You can however include the Json in your view for example in a Javascript variable like this:

...
var jsModelJSON = '@Html.Raw(JsonConvert.SerializeObject(Model)';
// or
var jsModelObject = @Html.Raw(JsonConvert.SerializeObject(Model);
...

with help of the Json.NET librarys converter method. Note that a valid Json inserted into javascript code should result a valid javascript object definition in the second line.

If you want to pass your entire model down and convert it to a javascript object, should add this code to your view.

<script>
  $(document).ready(function(){

     var myObject = @Html.Raw(Json.Encode(Model))

  }
</script>

That should give you a client side model. It doesnt have to happen on document ready, but I usually do something immediately with the model. You could just put it the code within script tags for your object to be globally accessible. You don't need to return JSON from your action. Just do this

 return View(project);

In this case it is Model. If you are using Razor:

@model <your namespace>.Project
<h2>Album: @Model.Title</h2>

You said that does not work for you - can you list code in the view? How do you render the view (RenderPartial, call via ajax, etc.)?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top