-1

Here is my code in the controller.

public JsonResult directory()
{
    List<string> alp = new List<string>();
    var alp1 = new List<directories>();
    string array = "";
    int a = 0;
    for (a = 0; a <= 25; a++)
    {
        int unicode = a + 65;
        char character = (char)unicode;
        string text1 = character.ToString();
        string url1 = "<a href='/Directories/?search=" + text1 + "' 'rel=" + text1 + "'>";
        string alpha = text1;
        alp.Add(url1);
        alphatxt.Add(alpha);
    }
    var alphaa = alp1.Add(new directories { arrary = url1, character = alphatxt });
    return Json(alphaa, JsonRequestBehavior.AllowGet);
}

public class directories
{
    public int a { get; set; }
    public int unicode { get; set; }
    public char character { get; set; }
    public string[] arrary { get; set; }
}

Outputs are getting by

alp.Add(url1);
alp.Add(alpha);

How can i call these two outputs outside the loop.

so that i will get my output through the return

Json(alphaa, JsonRequestBehavior.AllowGet);

But I dont know how to declare the output to the variable outside the loop.

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
KesaVan
  • 1,031
  • 16
  • 32
  • 1
    What are you trying to do, actually? Create a list of urls, one for each character in the alphabet? – vgru Jul 30 '14 at 07:42

3 Answers3

2

If you are trying to build a list of urls, one for each letter, then you can simply do something like:

public List<Directory> GetDirectories()
{
    var dirs = new List<Directory>();
    for (var ch = 'A'; ch <= 'Z'; ch++)
    {
        var url = string.Format(
            "<a href='/Directories/?search={0}' rel='{0}'>", ch);

        dirs.Add(new Directory() { Character = ch, Url = url });
    }
    return dirs;
}

// Directory class is simplifed a bit in this example
public class Directory
{
    public char Character { get; set; }
    public string Url { get; set; }
}

And then simply convert it to JSON in a separate method:

public JsonResult directory()
{
    var dirs = GetDirectories();
    return Json(dirs, JsonRequestBehavior.AllowGet);
}

Using LINQ, it could be simplified to:

private static readonly string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public List<Directory> GetDirectories()
{
    return Alphabet
        .Select(ch => new Directory() { Character = ch, Url = CreateUrl(ch) })
        .ToList();
}

private string CreateUrl(char ch)
{
    return string.Format("<a href='/Directories/?search={0}' 'rel={0}'>", ch);
}

The way your code looks right now, it doesn't seem like you need to create this list on the server side at all (you are transferring a bunch of almost equal hard-coded URLs, which can easily be created on the client side using JavaScript), so I presume there is some additional data you are transferring with this query?

vgru
  • 49,838
  • 16
  • 120
  • 201
  • Apparently the for loop is the quickest: http://stackoverflow.com/questions/2208688/quickest-way-to-enumerate-the-alphabet-in-c-sharp – MichaelLake Jul 30 '14 at 08:00
1

You can access the JsonResult.Data property, but I don't really think that is what you need. I suggest to create a method that return the actual result, and inside your action you call that one and serialize it as JSON:

public JsonResult directory()
{
    return Json(this.GetDirectories(), JsonRequestBehavior.AllowGet);
}

private List<directories> GetDirectories()
{
    ... // your original code
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
0

I tried in many ways, At last got idea to use this way,

Also My code is shown below.

public JsonResult directory()
        {


            List<string> alp = new List<string>();
            var alp1 = new List<directories>();
            string array = "";
            int a = 0;
            for (a = 0; a <= 25; a++)
            {
                int unicode = a + 65;
                char character = (char)unicode;
                string text1 = character.ToString();
                string url1 = "<a href='/Directories/?search=" + text1 + "' 'rel=" + text1 + "'>";
                string alpha = text1;
                alp.Add(url1);
                alp.Add(alpha);
                alp1.Add(new directories { dirurl = url1, text = alpha });
            }

            return Json(alp1, JsonRequestBehavior.AllowGet);
        }


        public class directories
        {

            public string text { get; set; }
            public string dirurl { get; set; }
        }


    }
}
KesaVan
  • 1,031
  • 16
  • 32