Solved

mvc jquery Json style

Posted on 2013-11-07
4
440 Views
Last Modified: 2013-11-13
The first static example everything returns corrrectly when I am using my dynamic example the Json data returns as one string with commas after each value.
 
public JsonResult GetSite()

{

   var sites = new[]

    {

new {Nome = "site 1" },    new {Nome = "site 3" },new {Nome = "site 4" },

    };

     return Json(sites, JsonRequestBehavior.AllowGet);

    } 

Open in new window

Now I have my Data Dynamically I am pulling from a model.
  public JsonResult mystore()
        {
                   List<string> ChainResultList = ListOfChain();
               var sites = new[]
    {
        new {Nome = ChainResultList},
       
    };
           return Json(sites, JsonRequestBehavior.AllowGet);

        }

Open in new window

But this returns everything as one long string. Or commas after the value.
I am assuming that new {Nome = ChainResultList}, needs to be with the new SelectList in order to appear correctly.
 new SelectList(ChainResultList)  $(function() {
                    $.getJSON('@Url.Action("GetSite", "Home")', function(result) {
                        var ddl = $('#MyList');
                   ddl.empty();
                   $(result).each(function (i, item) {
                       alert(this.Nome);
                       ddl.append(
                           $('<option/>', {
                               value: this.Id

                           }).html(this.Nome)

                       );
                   });
               });
           });
    });    
.

Open in new window

0
Comment
Question by:sevensnake77
  • 3
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39632210
Your two examples are not equivalent. The "static" example assigns strings to Nome; the "dynamic" example assigns List<string>s to Nome. Which is correct?
0
 
LVL 9

Author Comment

by:sevensnake77
ID: 39632251
The static is just an example. but I am actually using the dynamic. The dynamic one I like to pass to my json data but it is not coming out correct. string with commas. How can I pass it like the static.?
0
 
LVL 9

Author Comment

by:sevensnake77
ID: 39638929
Ok I found out i need to set up a datatext field and a datavalue field within the controller that is my problem.
0
 
LVL 9

Author Closing Comment

by:sevensnake77
ID: 39646296
tks
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question