tags:

views:

539

answers:

1

Trying to create a list to return some JSON data to a view. Following a few tutorials on the web that were created during the beta but it seems in the RC the code does work.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication6.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public JsonResult list()
        {
            List<ListItem> list = new List<ListItem> {
                new ListItem() {Name="value", Something = "more Values"}
            };
            return Json(List);
        }
    }
}

The problem is ListItem is not found in the System.Web.Mvc namespace. I can't seem to get this to work. If ListItem has been removed how do you accomplish this in Release Candidate MVC?

Here are the tutorials I am trying to use:

http://geekswithblogs.net/michelotti/archive/2008/06/28/mvc-json---jsonresult-and-jquery.aspx http://nayyeri.net/blog/using-jsonresult-in-asp-net-mvc-ajax/

+3  A: 

ListItem is in the System.Web.UI.WebControls...you probably don't want to use this. You could use SelectListItem which is in the System.Web.Mvc namespace, but you'll have to change the property names (you'd have to do this for ListItem as well).

    public JsonResult list()
    {
        List<SelectListItem> list = new List<SelectListItem> {
            new ListItem() { Text="value", Value = "more Values" }
        };
        return Json(List);
    }

I'd be tempted to do this with List<object> and anonymous types so that I could define the property names to my liking.

    public JsonResult list()
    {
        List<object> list = new List<object> {
            new { Name="value", Something = "more Values" }
        };
        return Json(List);
    }
tvanfosson
Any thoughts on how you would nest the JSON? So have the JSON look like this { tags:[{name:"value"}]} How can you insert the tags: into the JSON results?
RedWolves
I try something like, but I'm not sure if it would work. Have to look at the source for the Json formatter.var tags = new { tags = new List<object> { name = "value" } };return Json(tags);
tvanfosson