0

This is my first MVC project. I am building a Bing Map application (that loads multiple pushpins on the map).

Here's my Index ActionResult

public ActionResult Index(string Id)
{

    // Here is the code to populate the DataSet using Id parameter

    DataTable dtReport = ds.Tables[0];
    List<MapPoint> points = new List<MapPoint>();
    int index = 1;
    foreach (DataRow r in dt.Rows)
    {
        points.Add(GetPointInfo(r, false));
        index++;
    }

    //return the list as JSON
    return Json(points, JsonRequestBehavior.AllowGet);
}

My problem is, when I go to the Index view, all I see is the Json formatted data and the map disappears. I would assume this happens because I am returning JsonResult in the Index ActionResult.

Is there any way I can retain the map on the View and still be able to pass JsonResult to the Index view and access it using jQuery?

3
  • Are you using jQuery for sending an ajax request to the server and consuming the response using javascript? Something like this. If so, could you post the javascript in the client side? Commented Aug 6, 2013 at 19:09
  • Currently I use another method called GetLocations in the controller and using somthing like this: $.getJSON("/GetLocations/", stripNull({ Id: '090' }), function (data) { code here }); but my problem with this is I need to access URL parameter. So, I want to achieve everything using the Index view if possible. Commented Aug 6, 2013 at 19:13
  • Pass the json result in view data and you can use the view data result in javascript. make sure your action returns view and not json. Commented Aug 7, 2013 at 3:36

1 Answer 1

2

Just return the view,and serialize the data to json,then pass the json data to the view. Operate the json data with javascript in the page.

Sign up to request clarification or add additional context in comments.

1 Comment

Fixed it last night @shimrom. I used ViewBag to pass JSON data. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.